Any way to get find command o/p in chronological order?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Any way to get find command o/p in chronological order?
# 1  
Old 06-11-2012
Any way to get find command o/p in chronological order?

Hi friends,

I am using below script to gzip files after naming them in a particular order.
but I intend to name them in numerical order as per their timings(earlier updated fle with a smaller numeric extension than later updated),but this script is not working as planned.
please help with this.

Code:
 
func_grease_logs_gzip ()
{
v1=`ls -lrt | grep "gz$" | awk '{print $9}' | cut -d "." -f 3 | tail -1`
v2=1
find . -type f \( -name 'grease*' -a ! -name '*.gz' -a ! -newer ${REFERENCE} \) -print | while read FILENAME
do
  v1=`expr $v1 + $v2`
  v3=`echo  ${FILENAME} | cut -d "." -f 1,2`
  mv ${FILENAME} $v3.$v1
  gzip $v3.$v1
done
}

# 2  
Old 06-11-2012
Quote:
but this script is not working as planned
What does it do? Please give examples, including example before and after filenames.
You might find that using the current date and time as a suffix is easier to code.
# 3  
Old 06-11-2012
Hi Methyl,

script performs below operation

suppose i have below files in a directory
Code:
 
 
-rw-r--r--   1 asgadmin1 bea815    756669 Jun  4 19:53 greaseRetry.log.236.gz
-rw-r--r--   1 asgadmin1 bea815    766149 Jun  5 19:45 greaseRetry.log.237.gz
-rw-r--r--   1 asgadmin1 bea815    732137 Jun  6 19:36 greaseRetry.log.238.gz
-rw-r--r--   1 asgadmin1 bea815   1858851 Jun  7 13:29 greaseBatch.log.239.gz
-rw-r--r--   1 asgadmin1 bea815    675340 Jun  7 19:27 greaseRetry.log.240.gz
-rw-r--r--   1 asgadmin1 bea815    762870 Jun  7 20:01 greaseRetry.log.241.gz
-rw-r--r--   1 asgadmin1 bea815    190537 Jun  7 20:15 greaseRetry.log.242.gz
-rw-r--r--   1 bea815   bea815    802449 Jun  8 20:06 greaseRetry.log.243.gz
-rw-r--r--   1 bea815   bea815    774030 Jun  9 19:42 greaseRetry.log.244.gz
-rw-r--r--   1 bea815   bea815    774030 Jun  9 19:43 greaseRetry.log.1
-rw-r--r--   1 bea815   bea815    774030 Jun  9 19:44 greaseRetry.log.2
-rw-r--r--   1 bea815   bea815    774030 Jun  9 19:45 greaseRetry.log

till
Code:
-rw-r--r--   1 bea815   bea815    774030 Jun  9 19:42 greaseRetry.log.244.gz

all files are gzipped ,i intend to gzip other files which are not of todays date following sequence of last file(log.245,log.246,log.247) as mentioned in above code ,i.e from next file onwards last three files
Code:
-rw-r--r--   1 bea815   bea815    774030 Jun  9 19:43 greaseRetry.log.1
-rw-r--r--   1 bea815   bea815    774030 Jun  9 19:44 greaseRetry.log.2
-rw-r--r--   1 bea815   bea815    774030 Jun  9 19:45 greaseRetry.log

should be gzipped with following names

Code:
-rw-r--r--   1 bea815   bea815    774030 Jun  9 19:43 greaseRetry.log.245.gz
-rw-r--r--   1 bea815   bea815    774030 Jun  9 19:44 greaseRetry.log.246.gz
-rw-r--r--   1 bea815   bea815    774030 Jun  9 19:45 greaseRetry.log247.gz

but they should ne gzipped in chronological order i.e
greaseRetry.log.1 as greaseRetry.log.245.gz
greaseRetry.log.2 as greaseRetry.log.246.gz
greaseRetry.log as greaseRetry.log.247.gz

but when i am running this script files are not gzipped in sequence as per there update timings,i need to rename them in sequence as per there last update time
# 4  
Old 06-11-2012
Infortunately the suffix on the unzipped files does not seem to relate to the timestamp or we could have used that to increment the new suffix.

Armed with the knowledge that there are small number of files per run, the following construct should work in order to process the files in chronological order within the loop. Note that this is ls-hyphen-one (not ls-hyphen-ell). All we are doing extra is placing the output of find onto a ls line so we can sort by timestamp. This technique does not work if any filenames contain space characters.

Code:
ls -1tr $(find ..... ) 2>/dev/null | while read FILENAME
do

done

# 5  
Old 06-12-2012
thanks Methyl,
this code is placing the files in chronological order but here i am facing a issue

Code:
 
YYYYMMDD="`date +%Y%m%d`"       # Reversed date yyyymmdd
REFERENCE=/tmp/reference.${YYYYMMDD}.$$
touch -t ${YYYYMMDD}0000 ${REFERENCE}   # First thing today
v1=`ls -lrt | grep "gz$" | awk '{print $9}' | cut -d "." -f 3 | tail -1`
v2=1
ls -1tr $(find . -type f \( -name 'grease*' -a ! -name '*.gz' -a ! -newer ${REFERENCE} \) -print) 2>/dev/null | while read FILENAME
do
                v1=`expr $v1 + $v2`
                v3=`echo  ${FILENAME} | cut -d "." -f 1,2`
                echo  $v1
                mv ${FILENAME} $v3.$v1
  gzip $v3.$v1
done

after running above code its giving error
Code:
 
expr: non-numeric argument
./greaseBatch.
expr: syntax error
./greaseRetry.
expr: syntax error

expr syntax is not working ,i tried other way also like
Code:
 
 
v1=$(($v1 + $v2))

but this is also giving error

may be as i am cutting v1 from filename shell is not considering it as a no. but as a string,any way to get arround this.

shell used is ksh
# 6  
Old 06-12-2012
Try diagnostic echoes of the values of $v1 and $v2.
Quote:
v1=`ls -lrt | grep "gz$" | awk '{print $9}' | cut -d "." -f 3 | tail -1`
More likely that this line is picking up the wrong file. Are there other gzip files in this directory which do not start with "grease" ?
Perhaps try:
Code:
v1=`ls -1rt grease*.gz 2>/dev/null | cut -d "." -f 3 | tail -1`

# 7  
Old 06-12-2012
thanks methyl,
above problem is resolved.
but i am facing a issue for below command

Code:
 
ls -1tr $(find . -type f \( -name 'grease*' -a ! -name '*.gz' -a ! -newer ${REFERENCE} \) -print) 2>/dev/null

when
Code:
find . -type f \( -name 'grease*' -a ! -name '*.gz' -a ! -newer ${REFERENCE} \) -print

has any files in its o/p than ls -1rt command gives proper o/p in chronological order
but when

Code:
find . -type f \( -name 'grease*' -a ! -name '*.gz' -a ! -newer ${REFERENCE} \) -print

has no files in o/p than
Code:
 
ls -1tr $(find . -type f \( -name 'grease*' -a ! -name '*.gz' -a ! -newer ${REFERENCE} \) -print) 2>/dev/null

lists all the files and starts gzipping wrongly .
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort in chronological order

I am trying to sort a log file in chronological order to identify which ones did not process and still have an old (probably yesterday's) date. This is a sample of the file:flatf 010140 flatf Thu May 10 22:22:11 CST 2018 flats finished flatf 010142 flatf Thu May 10 22:31:25 CST 2018 flats... (4 Replies)
Discussion started by: wbport
4 Replies

2. Web Development

Invalid command 'Order', perhaps misspelled or defined by a module not included in the server config

I have Apache v2.0.63 on Solaris 10 (x86 on VM). While starting the apache server, facing the following exception - Invalid command 'Order', perhaps misspelled or defined by a module not included in the server configuration Googling says to add the entry LoadModule authz_host_module... (0 Replies)
Discussion started by: poga
0 Replies

3. Shell Programming and Scripting

egrep command and order

Hi All, Here is my question. I have two files, file1.txt and file2.txt. I need the line number (index number) of file2.txt where the words in file1.txt appear. But they have to be in the same order as file1.txt. In example, file1.txt Z K A ... T file2.txt W A Q R (6 Replies)
Discussion started by: senayasma
6 Replies

4. Shell Programming and Scripting

Sorting dates in chronological order

Hi forum. I'm hoping someone can help me out with this problem. I tried to search online but couldn't come up with an exact solution. I have the following data file: H|20-May-2011|MF_FF.dat|77164|731374590.96|1|1|731374590.96|76586|77164|578|2988|Y... (8 Replies)
Discussion started by: pchang
8 Replies

5. UNIX for Dummies Questions & Answers

Find command - result order

Hi! Could you please explain why the result order isn't in reverse time order as it is requestet by "xargs ls -ltr" command (ksh shell)? There are about 5000 files in dir. $ find . -name "*201010*" -print |xargs ls -ltr |tail -rw-r--r-- 1 oracle oinstall 54326 Nov 25 20:32... (2 Replies)
Discussion started by: laki47
2 Replies

6. Linux

Using sort command to get numeric ascending order

HI everyone, I am trying to use the unix sort command to get a list of numbers sorted in ascending order but having trouble in getting it to work. An example of this issue would be when i am trying to sort the following three number each on a different line "1" , "2" and "116" the sort command... (3 Replies)
Discussion started by: wali4813
3 Replies

7. UNIX for Dummies Questions & Answers

Execute "find . -exec grep" in desending Order

Hi Xperts, I've one query for you. Please help me solving this. Below command is taking long time to fetch names of the files which contain string "475193976" because folder contains millions of files. I agree that this is what this function suppose to do. Correct.. But can it be possible... (6 Replies)
Discussion started by: gav_dhiman
6 Replies

8. UNIX for Dummies Questions & Answers

Help - Find command with list of files modified descending Order

Hi, I would like to know the command to get the files order in descending order with "FIND" command. Appreciate your help Thanks (4 Replies)
Discussion started by: TonySolarisAdmi
4 Replies

9. UNIX for Advanced & Expert Users

Uploading files in chronological order

Thanks for your help. (3 Replies)
Discussion started by: circuit.muni
3 Replies

10. Shell Programming and Scripting

alphabetical order with out using sort command

hai, how can i sort a file alphabetically without using sort command (6 Replies)
Discussion started by: rahul801
6 Replies
Login or Register to Ask a Question