grep with date

I have few files in one directory as below and I require the files that were created today...

$ls -ltr
-rw-r--r--   1 abc      abc      0 Dec  5 17:34 file4.txt
-rw-r--r--   1 abc      abc      0 Dec  5 17:34 file5.txt
-rw-r--r--   1 abc      abc      0 Dec  7 17:34 file6.txt
-rw-r--r--   1 abc      abc      0 Dec  9 17:34 file7.txt
-rw-r--r--   1 abc      abc      0 Dec  9 17:34 file9.txt
-rw-r--r--   1 abc      abc      0 Dec  9 17:34 file9.txt
-rw-r--r--   1 abc      abc      0 Dec  9 17:34 file9.txt

script written

file_check()
{
  # files for todays date
  cd /abc/giris
  ls -latr *.txt | grep 'date "+%b %d"' >> check.log
  echo  'total number of files :'
  ls -latr *.txt | grep 'date "+%b %d"' | wc -l
}

not errors displayed while executing this script but at the same time no files were shown.

$ ksh script.ksh
total number of files :
       0

Could you please correct me where I made the mistake?

Thanks in advance.

Try

ls -latr *.txt | grep "$(date '+%b %e')"
1 Like

Keep in mind that the creation time is not really stored in the inode so you may get false positives.

Files that were created somewhere in the past but were modified today will also match your search criteria.

1 Like

It isn't? What's ctime then? ctime is updated by chmod and chown, but is still much closer than mtime, which changes every time you change the file in any manner...

Modifying CarloM's idea slightly to use ctimes instead of atimes:

ls -clatr *.txt | grep "$(date '+%b %e')"
1 Like

ctime is the change time, not the creation time. I agree there are better odds that ctime actually reflects the creation time but it's not guaranteed.

The "ctime" timestamp is rarely of any use. Some backup software changes it after backing up files. It also changes if you change the permissions of the file.
Stick to "mtime" for this purpose.

In your original post, the issue is in this command:

Anything between single quotes ' ' is just a fixed string. You probably intended to type backticks ` ` to ask the Shell to execute the command. However the $( command ) syntax is preferred nowadays.
Also the "date" command included the parameter "%d" which gives the day of the month with a leading zero. This would not match a "ls -la" display until tomorrow (the 10th). The parameter "%e" is the day of the month with a space fill when it is a single digit - perfect for matching "ls -la" format.

CarloM's answer is good and corrects the original bugs in your script.

For large directories there are more sophisticated (and faster) methods using "find".

1 Like

Thank alot guys.... It worked...

Cheers...!!!!!

$( ) doesnt work for me.. but works when its replaced by back tics..

i am using solaris 10