How to List the Last Five Modified Files in Linux?


In Linux, it is often necessary to find out the most recently modified files for various reasons, such as troubleshooting or auditing purposes. Listing the last five modified files is a common requirement that can be achieved using various Linux commands. In this article, we will explore some of the methods to list the last five modified files in Linux.

Note − Linux commands are case-sensitive.

Using ls Command

The ls command is one of the most commonly used commands in Linux to list files and directories. We can use the ls command to list the files in a directory sorted by modification time using the -t option. We can then use the “head” command to display the first five files from the list. The following command lists the last five modified files in the current directory −

$ ls -lt | head -n 6

Output

total 31654651
-drwx-r--r--    7 user  root   1.2K   11 Dec   11:22 linuxfile.py
-rw-r--r--      5 user  root   2.7M   10 Dec   15:26 script-linux.pdf
-rw-r--r--      1 user  root   9.2M   9 Dec    16:21 win_scp.pdf
-rw-r--r--      4 user  root   502K   8 Dec    10:20 class1.pdf
-rw-rw-rw-      1 user  root   2.0M   5 Dec    22:06 script23.jpg

The -l option displays the files in a long listing format, including file permissions, ownership, size, and modification time. The -t option sorts the files by modification time in descending order, with the most recently modified file at the top. The head command displays the first six lines, which include the column headers and the last five modified files.

Now we will see how to use “sort” command to list all the last five modified files. As shown below, we need to give the path to the directory and use the sort keyword with tail to fetch last five modified files,

$ find /home/cg/root/ -type f -printf '%T@\t%p
' | sort -n | tail -5

Output

1684171184.3445822090	/home/cg/root/6462638544588/main.py
1684171184.7965797450	/home/cg/root/6462695907cd6/main.py
1684171186.4405707830	/home/cg/root/646264dd4a983/main.lua
1684171186.9005682750	/home/cg/root/64625da89eee4/main.lua
1684171188.9325571980	/home/cg/root/6462694c393df/main.pl

Using find Command

The find command is another useful command in Linux for searching files and directories based on various criteria, such as name, size, and modification time. We can use the find command to search for all files in a directory and its sub directories that have been modified within the last 24 hours using the -m time option. We can then use the head command to display the first five files from the list. The following command lists the last five modified files in the current directory and its sub directories −

$ find . -type f -mtime -1 -print0 | xargs -0 ls -lt | head -n 6

Output

2494345    4  drwxr-xr-x   3  root  root     496  Dec 20 12:55 /root/home/shell.py
2490782    4  -rw-r--r--       1  root  root     418  Dec  8  03:54 /root/home/sh/remote.sh
2435566    4  drwxr-xr-x   3  root  root    4096 Dec 20 12:54 /root/home/shell3.sh
2494508    4  -rw-r--r--       1  root  root      436 Dec 20 12:54 /root/home/script/test3/sh
2344324    4  drwxr-xr-x   6  root  root    4096 Dec 20 12:53 /root/home/test.py

The find command searches for all files (-type f) in the current directory (.) and its sub directories that have been modified within the last 24 hours (-m time -1) and prints their names with null character termination (-print 0). The x args command reads the null-terminated list of file names from find and passes them as arguments to the ls command, which sorts the files by modification time and displays them in a long listing format (-lt). The head command displays the first six lines, which include the column headers and the last five modified files.

Now we will see how to use “stat” command to list all the last five modified files. As shown below, we need to give the path to the directory and use the sort keyword with head to fetch last five modified files,

$ find /home/cg/root -type f -exec stat --format '%Y %n' {} + | sort -nr | head -5 

Output

1684172257 /home/cg/root/646267076a666/demo.m
1684172255 /home/cg/root/64626d0533a41/testing.txt
1684172255 /home/cg/root/646267bba8bdf/main.pl
1684172254 /home/cg/root/646267eb29beb/demo.m
1684172250 /home/cg/root/64626d8526584/main.lua

Conclusion

In this article, we have explored two methods to list the last five modified files in Linux. The ls command is a simple and efficient way to list the most recently modified files in a directory. The find command is a more powerful and flexible way to search for files based on various criteria, including modification time whereas the stat command provides a detailed view.

Updated on: 01-Aug-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements