How to add Multiple Searches in AWK command
You can match on multiple patterns like so:
awk '/Jun/ || /July/ || /Aug/' <(ls -lrh)
This returns all matches for either Jun, July, or Aug.
You don't require the print
statement as that is awk
's default action.
As a general rule, it is a very bad idea to parse the output of ls. A better way to do what you want is
stat -c "%n %y" * | grep 2013-0[678]
Alternatively, check only the last field to protect against the unlikely case where the file name itself is a date:
stat -c "%n %y" *| awk '$NF ~ /2013-0[678]/'
From man stat
:
-c --format=FORMAT
use the specified FORMAT instead of the default; output a new‐
line after each use of FORMAT
%y time of last modification, human-readable
%n file name
ls
is a command to display information about files in a human readable. Do not use it in scripts.
The command to find files in a directory tree matching certain criteria is find
. If you have GNU find (i.e. non-embedded Linux or Cygwin) or FreeBSD find (i.e. FreeBSD or OSX), you can use -newermt
to match files that were last modified after a certain date.
find . -newermt '1 June' ! -newermt '1 September' -print
Add a year if you want to match files in a specific year, rather than the calendar year when the script runs.
If your find implementation doesn't have -newermt
, you'll have to make do with -newer
, which compares the date of the file that find
found with a fixed file. use touch
to create temporary files with the date that you want to use as a boundary.
dir=$(mktemp -d)
touch -d 201306010000 "$dir/from"
touch -d 201309010000 "$dir/to"
find . -newer "$dir/from" ! -newer "$dir/to" -print
rm -r "$dir"