Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Using ls Command for Recent Files
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.
Consider the example shown below as reference −
ls -ltr
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 Find Command with Time-Based Filters
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.
Method 1: Using -mtime Flag
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 −
find − The Linux command that helps us in finding files/folders
/path_of_directory − The path of the directory where you want to check for changes
-mtime − Flag for modified timestamp that tells us the last time file contents were modified
-1 − Means anything changed one day or less ago
-ls − List all matching files/folders with detailed information
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
Method 2: Using -newermt Flag
While the above command is effective, an alternate command that we can use with more readable syntax 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 format, 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
Additional Time-Based Options
You can also use other time variations with the find command −
| Option | Description | Example |
|---|---|---|
| -mtime -1 | Files modified within last 24 hours | find /home -mtime -1 |
| -mtime +7 | Files modified more than 7 days ago | find /home -mtime +7 |
| -newermt "yesterday" | Files newer than yesterday | find /home -newermt "yesterday" |
| -newermt "2024-01-01" | Files newer than specific date | find /home -newermt "2024-01-01" |
Conclusion
Linux provides multiple ways to find recently changed files. The ls -ltr command gives a quick overview of recent changes in a directory, while the find command with -mtime or -newermt flags offers precise time-based filtering. The -newermt option is more readable and flexible for specifying time ranges.
