Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-11-2012
Registered User
 
Join Date: Feb 2012
Posts: 49
Thanks: 2
Thanked 3 Times in 3 Posts
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
}

Sponsored Links
    #2  
Old 06-11-2012
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
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.
Sponsored Links
    #3  
Old 06-11-2012
Registered User
 
Join Date: Feb 2012
Posts: 49
Thanks: 2
Thanked 3 Times in 3 Posts
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
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
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

Sponsored Links
    #5  
Old 06-12-2012
Registered User
 
Join Date: Feb 2012
Posts: 49
Thanks: 2
Thanked 3 Times in 3 Posts
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
Sponsored Links
    #6  
Old 06-12-2012
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
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`

Sponsored Links
    #7  
Old 06-12-2012
Registered User
 
Join Date: Feb 2012
Posts: 49
Thanks: 2
Thanked 3 Times in 3 Posts
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 .
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
egrep command and order senayasma Shell Programming and Scripting 6 10-13-2011 05:52 PM
Sorting dates in chronological order pchang Shell Programming and Scripting 8 07-15-2011 07:20 PM
Find command - result order laki47 UNIX for Dummies Questions & Answers 2 01-20-2011 12:50 PM
Help - Find command with list of files modified descending Order TonySolarisAdmi UNIX for Dummies Questions & Answers 4 11-20-2008 12:24 PM
Uploading files in chronological order circuit.muni UNIX for Advanced & Expert Users 3 11-16-2008 09:21 AM



All times are GMT -4. The time now is 10:54 AM.