7

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.

Peter Mortensen
  • 1,029
  • 1
  • 8
  • 10
User123
  • 375
  • 1
  • 5
  • 17

5 Answers5

7

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`?

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • At least with recent versions of GNU `find`, you can skip the temp file and use time strings directly with the `-newerXY` test e.g. `find . -type f -newermt yesterday` – steeldriver Oct 08 '17 at 14:19
  • @steeldriver Ah, but `yesterday` would refer to "this time yesterday" which may cause files modified before midnight to be returned. – Kusalananda Oct 08 '17 at 14:23
  • @steeldriver `0` works though... – Kusalananda Oct 08 '17 at 14:29
  • getting error: `$ touch -d "$(date +%FT00:00:00)" /tmp/midnight touch: illegal option -- d usage: touch [-amc] [-t [[CC]YY]MMDDhhmm[.SS] | -r ref_file] file ...` – User123 Oct 08 '17 at 14:50
  • 1
    @User123 What Linux are you running? Are you using busybox? The `-d` flag is a POSIX standard flag. Oh well, change that line to `touch -t "$(date +%Y%m%d0000)" /tmp/midnight`. – Kusalananda Oct 08 '17 at 14:55
3

You can use stat(1) to get the modified time of a file, but this is not portable.

On Linux:

$ stat -c %y some_file
2017-09-23 10:24:09.880806666 +0200

$ date -d @$(stat -c %Y some_file) +%d-%m-%Y
23-09-2017

On BSD:

$ stat -f %Sm -t %d-%m-%Y some_file
23-09-2017
Satō Katsura
  • 13,138
  • 2
  • 31
  • 48
1

how to get the files of the current date?

ls command has --time-style option to print the time in specific format:

ls -l --time-style=+'%d-%m-%Y' | awk -v d=$(date +%d-%m-%Y) '$6==d'

--time-style=STYLE
with -l, show times using style STYLE: full-iso, long-iso, iso, locale, or +FORMAT; FORMAT is interpreted like in 'date';

RomanPerekhrest
  • 29,703
  • 3
  • 43
  • 67
1

With zsh, using glob qualifiers and age:

autoload age
print -rl ./**/XYZ.LOG*(e_'age today now'_)

or, if you prefer the long listing format:

autoload age
ls -lrtd -- **/XYZ.LOG*(e_'age today now'_)
don_crissti
  • 79,330
  • 30
  • 216
  • 245
  • Note that it will not list files with `mtime` in the future but still today, so after whatever `now` is when you run the command until `00:00:00` tommorow. To get those too remove the argument `now`. – don_crissti Oct 08 '17 at 15:33
0

Did you try:

echo `date +'%Y-%m-%d'`*