In this article, we will discuss how to find files in Linux / Unix, that are modified in last few minutes using the find command.

Table of Contents

Many times, we encounter the scenarios where we need to find files which are modified in last few minutes. Now, these last few minutes can be the last 5 minutes or last 30 minutes or last N minutes. In such scenarios we need to search files by the last modified time. For this, we can easily use the find command in Linux.

Overview of find command in Linux

The find command in Linux, provides an easy way to search files recursively in a directory hierarchy. It also provides various options to do the selective search. One such option is “-mmin n”, it helps to recursively search files by modified time,

Syntax of find command with “-mmin n” option

find <directory> -type f -nmin n

It will recursively look for the files which are modified less than, more than or exactly n minutes ago in the given directory. More details about the -nmin option of find command is as follows,

-mmin n

  • Search for files which are last modified less than, more than or exactly n minutes ago.
  • Here, n is a numeric argument and its value can be,
    • -n : find command will look for files modified in last n minutes.
    • +n : find command will look for files modified in before the last n minutes i.e. which are not modified in last n mins.
    • n : find command will look for files which are modified exactly n minutes ago.

Find files modified in last 5 minutes in Linux

To find all the files modified in last 5 minutes, we need to use the find command with -nmin option and numeric argument will -5. For example,

find myapp/ -type f -mmin -5

Output:

myapp/config.ini
myapp/sample.log

It recursively search for the files inside folder myapp/ and returned all the files which are modified in last 5 minutes. It is important to specify the -ve sign along with 5 because then only it will look for files modified in last 5 minutes. You can change the folder path based on your requirements.

The option ‘-type f’ is to make sure that it selects the files only, if also you want to see the directories which are modified in last 5 minutes, then you can skip this option.

If you want the complete details of the files, then append the ‘ls’ command too. For example,

find myapp/ -type f -mmin -5 -ls

Output:

   512772      4 -rw-rw-r--   1 ubuntu   ubuntu          2 Jan 16 16:35 myapp/config.ini
   512820      4 -rw-rw-r--   1 ubuntu   ubuntu          2 Jan 16 16:35 myapp/sample.log

Linux: Find files changed in last 10 minutes

To find all the files modified in last 10 minutes, we need to use the find command with -nmin option and numeric argument will 10. For example,

find myapp/ -type f -mmin -10 -ls

Output:

   512772      4 -rw-rw-r--   1 ubuntu   ubuntu          2 Jan 16 16:35 myapp/config.ini
   512820      4 -rw-rw-r--   1 ubuntu   ubuntu          2 Jan 16 16:35 myapp/sample.log
   512836      0 -rw-rw-r--   1 ubuntu   ubuntu          0 Jan 16 16:28 myapp/sample3.log

It recursively search for the files inside folder myapp/ and returned all the files which are modified in last 10 minutes. It is important to specify the -ve sign along with 10 because then only it will look for files modified in last 10 minutes.

Linux: Find files changed in last N minutes

So, to search files modified in last N minutes we need to pass the -N as the numeric argument to -nmin option of the find command,

find myapp/ -type f -mmin -N -ls

It recursively search for the files inside folder myapp/ and returned all the files which are modified in last N minutes. Where N can be any numeric value.