Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to get the list of files(.log/.lst) present in a directory($logfolder) whose creation date is in a specific $year and $month

stat  --format='%y %n'  $logfolder/* |
  grep "$year-$month-"|
  awk -F' ' '{print $4}'|
  grep 'log$\|lst$' > $archivepath/filesToArchive

This doesnt work when I query the command to a folder where there are too many files. I get the following error:

-bash: /usr/bin/stat: Argument list too long

Could anybody please help me?

share|improve this question
    
Your grep will catch both files with the date in the name and the stat date. –  mtm Jun 25 at 16:39
    
Your awk will miss files with spaces in the name. –  mtm Jun 25 at 16:40
add comment

4 Answers 4

For a funny possibility, if your find handles -newerXY, use it! For example to get the files from year 1977 and month October:

find "$logfolder" \( -name '*.log' -o -name '*.lst' \) -newermt "1977-10-01" \! -newermt "1977-10-01 +1 month"

Done!

As you already have the variables year and month it's straightforward to write as:

find "$logfolder" \( -name '*.log' -o -name '*.lst' \) -newermt "$year-$month-01" \! -newermt "$year-$month-01 +1 month"

Only one find command! amazing!

share|improve this answer
1  
+1 Since the OP's stat is of the GNU variant, his find more than likely supports -newermt as well. –  Stéphane Chazelas Jun 25 at 18:44
add comment

I would do it like this:

find "$logfolder" \( -name '*.log' -o -name '*lst' \) -printf "%TB\t%TY\t%p\n" |
     awk '$1==m && $2==y' m="$month" y="$year" | cut -f 3- 

Explanation

By grouping the two -name calls in parentheses, you can combine them with the -o (or) flag. This will make find look for either .log or .lst files. The -printf (a GNU extension) prints the file's modification month (%TB), then its modification year (%TY) and then its path (%p), with a tab (\t) between each field.

The awk simply checks that the 1st field (the month) is the same as $month and the second is the same as $year.

The cut removes the first two fields (the month and year) and prints everything from the 3rd field on.

I tested the above by creating files modified in December 2012 (and set $month to "December" and $year to 2012):

$ touch -d "December 13 2012" {a,b,c}{.lst,.log}
$ touch c.lst a.log ## c.lst and a.log now have today's modification date.
$ find $logfolder \( -name '*.log' -o -name '*lst' \) -printf "%TB\t%TY\t%p\n" |
  awk '$1==m && $2==y' m="$month" y="$year" | cut -f 3-
./b.log
./c.log
./b.lst
./a.lst

(note that it assumes file and directory names don't contain newline characters).

share|improve this answer
add comment

Try this:

find $logfolder -type f -exec stat --format='%y %n' "{}" + |
  grep "$year-$month-"|
  awk -F' ' '{print $4}'|
  grep 'log$\|lst$' > $archivepath/filesToArchive
share|improve this answer
    
grep "^$year-$month-" so you don't the string in file names –  mtm Jun 25 at 16:29
    
@mtm: I only use find to fix the error Argument list too long. It's very hard to know exactly what the OP want. –  Gnouc Jun 25 at 16:33
    
No worries.. will comment on OP –  mtm Jun 25 at 16:39
    
awk can do grep's job and more, you almost never need to pipe awk and grep together. Note that your last grep command is not portable/standard. –  Stéphane Chazelas Jun 25 at 18:50
add comment

ls -lh *.log *.lst logfolder | grep year | grep month

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.