I think these ls
commands are far better than using find if the additional file metrics that find
(which just returns filenames) does not provide are needed. That said, all the ls solutions seem really cumbersome piping to awk. Why not just use gnu date's built in formatting instead?
ls -ltr directory/ | grep "$(date +"%b %e")"
Does the same thing and requires no awk print statements or conditionals. GNU date
formatting is really helpful for outputting your date exactly how you need. In this case %b matches the 3 letter month and %e is a space-padded day value matching the format of ls instead of a 0-padded day value that the default date
uses.
ls -ltr directory/ | grep "$(date +"%b %e")"
Example output:
-rw-r----- 1 ocams ocams 987 Sep 2 01:45 ember_status_2021-245-01_30.log.gz.closed
-rw-r----- 1 ocams ocams 1202 Sep 2 01:45 realmvm_status_2021-245-00_04.log.gz.closed
-rw-r----- 1 ocams ocams 1085 Sep 2 01:45 realmvm_status_2021-245-01_04.log.gz.closed
-rw-r----- 1 ocams ocams 312590 Sep 2 01:45 3-21-244-234712.csv.gz.closed
-rw-r----- 1 ocams ocams 925880 Sep 2 01:45 1-21-245-010728.csv.gz.closed
-rw-r----- 1 ocams ocams 310556 Sep 2 01:45 3-21-245-010238.csv.gz.closed
-rw-r----- 1 ocams ocams 1041 Sep 2 01:45 ember_status_2021-245-00_45.log.gz.closed
If you wanted to more closely match find
to get exactly 24 hours you can handle the two different dates (the current day, and the previous day) with an or condition in grep: ls -ltr directory/ | grep "$(date +"%b %e")\|$(date -d -1day +"%b %e")"
. This is returning 25-48 hours worth of data instead of <24.
ls -ltr directory/ | grep "$(date +"%b %e")\|$(date -d -1day +"%b %e")"
Example output:
-rw-r----- 1 ocams ocams 314951 Sep 1 23:45 3-21-244-231707.csv.gz.closed
-rw-r----- 1 ocams ocams 899348 Sep 1 23:45 1-21-244-230205.csv.gz.closed
-rw-r----- 1 ocams ocams 915400 Sep 1 23:45 1-21-244-231708.csv.gz.closed
-rw-r----- 1 ocams ocams 671063 Sep 1 23:45 2-21-244-231708.csv.gz.closed
-rw-r----- 1 ocams ocams 666953 Sep 1 23:45 2-21-244-230205.csv.gz.closed
-rw-r----- 1 ocams ocams 987 Sep 2 01:45 ember_status_2021-245-01_30.log.gz.closed
-rw-r----- 1 ocams ocams 1202 Sep 2 01:45 realmvm_status_2021-245-00_04.log.gz.closed
-rw-r----- 1 ocams ocams 1085 Sep 2 01:45 realmvm_status_2021-245-01_04.log.gz.closed
ls
lists files, but it doesn't offer much to select what files to list or the output format. Use globbing (shell wildcards) orfind
when you want to select files by name or metadata (e.g. date).zsh
:ls -1 -- *(.m-1)