Linux – Getting files for the current date in Linux

datefileslinuxtimestamps

Usually the date +%d gives the output 08 for the current date, 08/10/2017. But when I do the ls -lrt on a path, the date format is like Oct 8 15:03, so, how do I get the files of the current date?

I'm using the command

ls -lrt XYZ.LOG* |grep "$(date +'%b %d')" |awk '{print $9}'

but it's not giving me the file of today's date (08/10/2017) although it gives me correct output for the dates 10 – 31st of any month.

Best Answer

This is cheating a bit, but it works.

First create an empty reference file with a specific timestamp, namely midnight:

touch -d "$(date +%FT00:00:00)" /tmp/midnight

Then find files that are newer than this file:

find . -type f -newer /tmp/midnight

If you want ls-like output from find rather than just the pathnames:

find . -type f -newer /tmp/midnight -ls

If you want to find files matching the pattern XYZ.LOG*:

find . -type f -name 'XYZ.LOG*' -newer /tmp/midnight -ls

If you have GNU find, you may bypass the temporary file and use

find . -type f -newermt 0

to get files modified since midnight.


Related: Why *not* parse `ls`?

Related Question