Find shell command

From Notes_Wiki
Revision as of 13:35, 7 April 2022 by Saurabh (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Home > Shell scripting > find shell command

Find files older than given no. of days

To find files older than 30 days use:

find . -mtime +30 -daystart

Careful To automatically delete files older than 30 days use:

find . -mtime +30 -daystart -delete

To find files modified within last 5-6 days use:

find . -mtime +5 -mtime -6

There is also atime and ctime for access times and change times respectively. mtime used in above examples is for modification times.


Find files between two given dates

To find files between two given dates or timestamps use commands similar to:

find -newerct "1 Aug 2013" ! -newerct "1 Sep 2013" -ls
find -newermt "2017-11-06 17:30:00" ! -newermt "2017-11-06 22:00:00" -ls

Refer: https://stackoverflow.com/questions/18339307/find-files-in-created-between-a-date-range

To find files between given dates and grep for a string and print file names having that string use:

find . -newermt "1 Oct 2016" ! -newermt "31 Oct 2016" -type f -print0 | xargs -0 grep -l impstring > imp-files.txt 2>&1 &

Here:

  • m in newermt stands for modification time. There are other options such as -newerct for creation time or -newerat for access times, etc.
  • -print0 in find prints search results terminated by null. This is great if you want to deal with spaces, special characters, etc.
  • xargs -0 takes inputs given by find -print0 appropriately. No need to worry about escaping the special characters eg spaces for grep. xargs will take care of that


Home > Shell scripting > find shell command