How to find the files in Linux that have been changed in the last 24 hours?


One thing that is constant about working with Linux is that we will make changes to one file or another with time. There are some files that stay unchanged, for example the files inside the /usr/local/ directory while there are some files that are just temporary and get deleted automatically, like the files or folders you insert in the /tmp directory.

Since we know that change is imminent to files and folders, Linux also provides us with different ways to keep track of the files or folders that we change or have changed.

The most common way to check if we have changed any file in recent time is just to list the files in a certain order.

In Linux, we list the files with the help of the ls command and when we use the flag - ltr along with the ls command then we get the list of files and folders, where the recently updated files or folders are present at the bottom of the list.

Let’s explore an example of the same, where I will print the current directory using the ls -ltr command.

Consider the example shown below as reference −

ls -ltr

Output

immukul@192 check % ls -ltr
total 4624
-rw-r--r-- 1 immukul staff 1132    Jun 2 11:45 cpu.profile
-rwxr-xr-x 1 immukul staff 2340960 Jun 2 13:11 app
-rw-r--r-- 1 immukul staff 856     Jun 8 10:35 x.profile
-rw-r--r-- 1 immukul staff 90      Jun 8 16:31 go.mod
-rw-r--r-- 1 immukul staff 888     Jun 8 17:47 go.sum
-rw-r--r-- 1 immukul staff 8149    Jun 8 19:07 main.go

In the above output, you can clearly notice that the -ltr command prints the lastly modified files or folders at the bottom of the terminal output.

Using the -ltr flag along with ls command is one way to check which files/folders were changed recently. But in order to get the files/folders that were changed in a particular timeframe we need to make use of different commands that linux provides us with.

Linux provides us with a very powerful command known as find which we use when we want to find certain files/folders, but this command can be clubbed along with certain flags to get the files/folders that were changed in a certain duration.

To get the files/folders that were changed in the past 24 hours, we need to write the following command to the terminal −

find /path_of_directory -mtime -1 -ls

Let’s break the above command and understand what everything means or refers to.

The find keyword is the obvious Linux command that helps us in finding a certain file/folder, followed by the /path_of_directory which is nothing but the path of the directory where you want to check the files/folders that have changed in the past 24 hours. After that, we have the flag -mtime which stands for modified timestamp that tells us the last time the contents of a file were modified, and then we have -1, it means that anything changed one day or less ago and lastly we are using the -ls command to list all the files/folders that were changed in the past 24 hours. When we run the above command in a terminal, we can expect an output like this −

Output

When we run the above command in a terminal, we can expect an output like this −

immukul@192 ~ % find /Users/immukul/Downloads/ -mtime -1 -ls
1121822 0 drwx------ 216 immukul staff 6912 Jul 3 11:22
/Users/immukul/Downloads/
1140249 80 -rw-r--r-- 1 immukul staff 38916 Jul 3 11:22
/Users/immukul/Downloads//.DS_Store
21671276 56 -rw-r--r-- 1 immukul staff 26534 Jul 3 11:15
/Users/immukul/Downloads//Linux-Topics.txt

We can clearly notice that all of the files listed in the above output have changed in the past 24 hours.

While the above command to list the files/folders changed in the past 24 hours is a bit hard to remember, an alternate command that we can use that does that same trick is shown below −

find /path_of_directory -newermt "-24 hours" -ls

In the above command we replaced the -mtime with -newermt and instead of writing -1 we simply wrote the time in a string, and it works like a charm.

immukul@192 ~ % find /Users/immukul/Downloads -newermt "-24 hours" -ls
1121822 0 drwx------ 216 immukul staff 6912 Jul 3 11:22
/Users/immukul/Downloads
1140249 80 -rw-r--r-- 1 immukul staff 38916 Jul 3 11:22
/Users/immukul/Downloads/.DS_Store
21671276 56 -rw-r--r-- 1 immukul staff 26534 Jul 3 11:15
/Users/immukul/Downloads/Linux-Topics.txt

Updated on: 29-Jul-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements