find is the Unix/Linux command line utility used for searching files across the file system. Sometimes we need to search files modified in last few days. Assume you have modified multiple files in your application and forgot to keep track of the modified files. In that case, find command provides you an option to search files based on their modification. You can also search the files modified before X days.

Advertisement

Use -mtime option with the find command to search files based on modification time followed by the number of days. Number of days can be used in two formats.

  1. Use + with number of days to search file modified older that X days
  2. Use with number of days to search file modified in last X days

The below examples will help you to understand the search for files based on modification time.

Find files modified in last X days

Use below command to search all files and directories modified in last 30 days. Here dot (.) is used to search in current directory. And -30 defines to search files modified in last 30 day. Change this number with your search requirements.

find . -mtime -30

You can also customize search based on file type. Use -type followed with -f (file) or -d (directory). Below command will search for files only.

find . -type f -mtime -30

Find files modified before X days

The below command will search all files and directories modified before 30 days. Here dot (.) is used to search in current directory. And +30 defines to search files modified before 30 day. Change this number with your search preferences.

find . -mtime +30

Customize search pattern to search for files only using -type f. Or use -type d to search for directories.

find . -type f -mtime +30

Conclusion

This tutorial described you to find files based on modification days. You can also use more options with find command to filter more.

Share.

4 Comments

  1. Hello

    In the find manual, you can read:
    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.
    Go to know, isn’t it ?
    Cheers

  2. All of this was broken with systemd.

    $ find . -mtime -30 days
    find: paths must precede expression: days
    Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path…] [expression]

Leave A Reply