1. Overview

Managing files efficiently is crucial in maintaining a functioning operating system. Sorting files in a directory based on their last modified date can be extremely helpful for organizing and accessing files.

We’re usually in the habit of saving a lot of information in the form of files on our system. This includes all hidden and unhidden files in the current working directory and any subdirectories.

Linux has a list of commands that we can use separately or in combination to search for files or sort a collection of files according to their modification date or even the creation date.

In this tutorial, we’ll explore various Linux commands that allow us to recursively sort files in a directory based on their last modified date.

2. Using find Command

The find command is a powerful command line utility for scanning through file hierarchies in UNIX-based operating systems. We can use it to search for files or directories and perform additional operations on them.

It supports searching through different ways such as name, directory, modification date, creation date, permission, and more. We can also pass the -exec option to execute more commands on the files or directories found.

We’ll explore different combinations of the find command and other Linux commands to recursively sort files in a directory based on the last modified date.

Let’s look at the first combination of commands:

$ find ./ -type f -exec ls -lt --time-style=+"%Y-%m-%d %T" {} + | sort -k6,7
-rwxrw-r-- 1 user user 165 2023-04-01 15:06:36 ./perl_c.sh
-rw-rw-r-- 1 user user 21 2023-04-03 13:25:21 ./data.txt
-rw-rw-r-- 1 user user 20 2023-04-03 13:26:53 ./data2.txt
-rw-rw-r-- 1 user user 0 2023-04-20 19:32:26 ./sample.txt
-rw-rw-r-- 1 user user 32 2023-04-20 19:35:12 ./sample.txt.enc
.... truncated ....

Here, the find command locates all files recursively in the current directory. The -type f  option ensures that only files, not directories, are considered.

The -exec option executes the subsequent ls command on each file. The -lt option displays file details, including the last modified date. The –time-style parameter formats the date output in the format year, month, day, and time. Finally, we pipe the output to the sort command with the -k6,7 option that sorts the files based on the 6th and 7th columns. These columns represent the last modified date and time.

In the second combination, we’ll use a combination of the Linux find, stat, and sort commands:

$ sudo find ./ -type f -exec stat -c "%Y %n" {} + | sort -k1n
1675321543 ./ba.txt
1680350796 ./perl_c.sh
1680517521 ./data.txt
1680517613 ./data2.txt
1682008346 ./sample.txt
1682008512 ./sample.txt.enc
.... truncated ....

Here, the find command locates all files recursively, and the -type f option ensures that only files are considered. The -exec option executes the stat command on each file and retrieves the last modified date as an epoch time in the first column and the file path in the second column.

Finally, we pipe the output to the sort command that arranges the files based on the first column numerically (epoch time), resulting in the files being arranged in ascending order based on the last modified date.

2.1. Finding Files Last Modified Within a Specific Period

We can also use the find command to find and sort files that were last modified within a specific period.

For example, let’s search for files modified within the last 60 minutes:

$ find ./ -type f -mmin -60
./ba.txt
./filetwo.txt
./data.txt
./filethree.txt
.... truncated ....

We’re using the -mmin option to specify 60 minutes.

Alternatively, we can use the -mtime option to specify the number of days:

$ find ./ -type f -mtime -2
./data.txt
./sample.txt.enc
./        Accounting Dimensions                         Cost Cente.txt
./perl_c.sh
.... truncated ....

We’re using the -mtime option to search for files modified within the last two days.

However, the methods above list all files within the current directory and all subdirectories. We can limit the subdirectory level depth to search for files.

Let’s list all files modified within the last five days and search no more than three levels deep:

$ find ./ -maxdepth 3 -type f -mtime -5
./data.txt
./sample.txt.enc
./        Accounting Dimensions                         Cost Cente.txt
./perl_c.sh
.... truncated ....

We’re passing the -maxdepth option to limit the subdirectory search depth level.

3. Using ls Command

By default, the Linux ls command displays a list of all files and subdirectories in the current directory.

We can combine the ls command with the grep, sort, and cut commands to sort files in a directory recursively:

$ ls -ltR --time-style=+"%Y-%m-%d %T" ./ | grep -v '^d' | sort -k6,7 | cut -d' ' -f6-

./:
./FFmpeg:


  0 2023-02-02 10:05:43 ba.txt
  0 2023-02-02 10:05:43 file_1.txt
  0 2023-02-02 10:05:43 filethirteen.txt
.... truncated ....

Here, the ls command, with the -ltR option, lists files recursively, including their last modified date in the specified time style. The grep command uses the -v option, which filters out lines starting with “d” (directories) to include only files. The sort command arranges the files based on the 6th and 7th columns, representing the last modified date and time.

Finally, the cut command removes the first five columns, leaving only the file names.

Alternatively, we can also use this command to list the most recently modified files:

$ ls -Art | tail -n 5
data.txt
data2.txt
sample.txt
sample.txt.enc
        Accounting Dimensions                         Cost Cente.txt
.... truncated ....

The -A option lists all the files in the current directory, and the -r option reverses the list. Finally, the -t option sorts the files based on the modification time.

4. Using GUI

We can also sort files and directories recursively based on the last modified date through the Graphical User Interface.

This approach may be more suitable for users without a lot of experience using the terminal. Moreover, it’s more visual, making it easier to search for a specific file.

To sort files in a directory through the GUI, let’s first open the directory using the file manager:

file manager

From the screen above, we can see the file details split into four different columns. The third column represents the last modified date or time for the corresponding file or directory.

We can sort the files by clicking the “Modified” column head.

An arrow pointing up means the files are in ascending order, and vice versa for an arrow pointing down:

ascending order

Here the files are sorted in ascending order.

However, this method limits us since we can’t list files within subdirectories or specify a time frame to filter files based on the last modified date.

5. Conclusion

In this article, we explored different methods of recursively sorting files in a directory based on the last modified date. In the first method, we used the find command and combined it with other Linux commands to sort the files in the working directory. We saw that this method gives us more flexibility and control over filtering the files.

In the second method, we combined the ls command with other commands to sort the files. We learned that we could also use it alone to achieve similar results, i.e., to only display the file names.

Finally, we looked at the GUI method, which is more visual and easier for users that don’t have experience using the terminal.

Comments are closed on this article!