1. Introduction
There are various occasions when we want to search for files that have been changed recently.
For example, as a system admin, we’re responsible to maintain and configure computer systems. Sometimes, because we’re dealing with a lot of configuration files, we probably want to know what are the files recently modified.
In this tutorial, we’re going to find the files that have been changed recently in Linux using bash commands.
2. The find Command
First, we’ll explore the find utility which is the most common way to achieve the intended purpose. This command is used to find files and directories recursively and to execute further operations on them.
2.1. -mtime and -mmin
-mtime is handy, for example, if we want to find all the files from the current directory that have changed in the last 24 hours:
find . -mtime -1
Note that the . is used to refer to the current directory. -mtime n is an expression that finds the files and directories that have been modified exactly n days ago.
In addition, the expression can be used in two other ways:
- -mtime +n = finds the files and directories modified more than n days ago
- -mtime -n = finds the files and directories modified less than n days ago
In the same way, we can use the -mmin n expression to rely on minutes instead of days:
find /home/sports -mmin +120
So, this command recursively finds all the files and directories from the /home/sports directory modified at least 120 minutes ago.
Next, if we want to limit the searching only to files, excluding directories, we need to add the -type f expression:
find /home/sports -type f -mmin +120
Furthermore, we can even compose expressions. So, let’s find the files that have been changed less than 120 minutes ago and more than 60 minutes ago:
find . -type f -mmin -120 -mmin +60
2.2. -newermt
There are times when we want to find the files that were modified based on a particular date. In order to fulfill this requirement, we have to explore another parameter, which has the following syntax:
-newermt 'yyyy-mm-dd'
By using this expression, we can get the files that have been changed earlier than the specified date.
So, let’s build a command to better understand the new parameter:
find . -type f -newermt 2019-07-24
Moreover, we could get the files modified on a specific date by using a composed expression.
So, we’re going to get the files modified on ‘2019-07-24’:
find . -type f -newermt 2019-07-24 ! -newermt 2019-07-25
Finally, there’s another version of the -newermt parameter similar to -mmin and -mtime.
The first command finds the files modified in the last 24 hours. The rest of them are similar:
find . -type f -newermt "-24 hours"
find . -type f -newermt "-10 minutes"
find . -type f -newermt "1 day ago"
find . -type f -newermt "yesterday"
