Skip to main content
added 42 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

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 modificatinmodification year (%TY) and then its namepath (%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

To get the names alone, you could pipe(note that throughit assumes file and directory names don't contain newline characters).

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 prints the file's modification month (%TB), then its modificatin year (%TY) and then its name (%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

To get the names alone, you could pipe that through

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).

added 1 character in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

I would do it like this:

find "$logfolder" \( -name '*.log' -o -name '*lst' \) -printf "%TB\t%TY\t%p\n" |
     cut -f 3- | 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 prints the file's modification month (%TB), then its modificatin year (%TY) and then its name (%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" |
 cut -f 3- | awk '$1==m && $2==y' m="$month" y="$year" | cut -f 3-
./b.log
./c.log
./b.lst
./a.lst

To get the names alone, you could pipe that through

I would do it like this:

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

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 prints the file's modification month (%TB), then its modificatin year (%TY) and then its name (%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" |
 cut -f 3- | awk '$1==m && $2==y' m="$month" y="$year"
./b.log
./c.log
./b.lst
./a.lst

To get the names alone, you could pipe that through

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 prints the file's modification month (%TB), then its modificatin year (%TY) and then its name (%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

To get the names alone, you could pipe that through

Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

I would do it like this:

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

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 prints the file's modification month (%TB), then its modificatin year (%TY) and then its name (%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" |
 cut -f 3- | awk '$1==m && $2==y' m="$month" y="$year"
./b.log
./c.log
./b.lst
./a.lst

To get the names alone, you could pipe that through