, ,

Unix – Find Files By Age And Timestamp

Find files by date and regex

find the files modified more than 3 days ago …
good candidates for a clean up


sudo find . -iregex ".*\(bootcd\|src\).*.zip" -type f -mtime +30 -exec ls -l "{}" \;



Finding the newest files

# find text files changed today containing the text "gugu"
find . -mtime 0 -type f -name "*.txt" -exec grep --color -rHnE 'gugu' {} +

# find files modified in the last 40 minutes. mtime= days
find . -mtime -$(echo "40/(24*60)"|bc -l) -type f

# find files modified in the last 40 minutes. mmin = minutes
find . -mtime -40 -type f

# find all files changed in the last 30 minutes
find . -mtime -0h30m  -regextype posix-extended -iregex ".*.py|.*.java|.*.txt|.*.sh"


# find files modified in the last 24 hrs
find . -mtime 0 -type f

# find files modified SINCE the last 3x24 hrs
find . -mtime -3 -type f

# find files modified more than 3 days ago
find . -mtime +3 -type f

# find files modified SINCE the last 3x24 hrs
# alternative implementation using for loop - not clever really, but my first attempt
for i in {0..3}; do find . -mtime $i -type f; done;

# find the newest version of some_file containing pattern
find -newer some_file -type f -exec grep --color 'pattern' {} +

# find the recently modified files containing gugu
find -newer . -type f -exec grep --color -inrH 'gugu' {} +

# find files last access yesterday and containing the keyword pattern
find . -atime 1 -type f -exec grep 'pattern' {} +

# find files last modified within the last 15 days and having the extension .txt, case insensitive
find . -mtime -15 -type f -iname "*.txt"

# find files last modified within the last 30 days and having the extension .txt and text "gugu"
find . -mtime -15 -type f -iname "*.txt" -exec grep --color -rHnE 'gugu' {} +

The timestamp can alternatively be specified directly in date -d format and use other find tests e.g., -name, -mmin.

find . -mtime 0 -type f -exec grep --color -n 'gugu' {} +
> man find
...
  -amin n
              File was last accessed n minutes ago.
  -mmin n
              File's data was last modified n minutes ago.

  -mtime n
              File's data was last modified n*24 hours ago.
              See the comments for -atime to understand how  rounding  affects
              the interpretation of file modification times.
  -atime n
              File  was  last  accessed n*24 hours ago.  When find figures out how many 24-hour periods ago
              the file was last accessed, any fractional part is ignored, so to match -atime +1,
              a file has to have been accessed at least  two days ago.
 -newer file
              File was modified more recently than file.
              If file is a symbolic link and the -H option or the -L option is in effect,
              the modification time of the file it points to is always used.

References

1. Find and Grep: http://stackoverflow.com/questions/8851667/grep-files-based-on-time-stamp
2. For Loop: http://stackoverflow.com/questions/169511/how-do-i-iterate-over-a-range-of-numbers-in-bash
3. Find Arguments: man find
4. Use of atime: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_07.html
5. Using bc to execute floating point arithmetic operations: http://stackoverflow.com/questions/11039876/multiplication-on-command-line-terminal-unix
6. Bash Arithmetic: http://linux.about.com/od/Bash_Scripting_Solutions/a/Arithmetic-In-Bash.htm