Skip to Content

How to Sort Files by Last Modified Date in Linux

  • The -t option of the ls command sorts the output by modification time, with the newest ones first. The -r option reverses the order of sorting.
  • The -l option of the ls command shows more details about the files and directories, such as size, permissions, owner, group, and modification time.
  • The article also covers how to sort by other criteria such as size or name, how to sort only files or only directories, and how to answer some common questions related to sorting in Linux.

If you want to see the most recent changes in your files and directories, you might want to sort them by the last modified date. This can be useful for finding the latest versions of your documents, backups, logs, or any other files that you frequently update. In this article, we will show you how to sort the output of the ls command by last modified date in Linux using some simple options and examples.

What is the ls command?

The ls command is one of the most basic and commonly used commands in Linux. It stands for “list” and it displays the contents of a directory or file. By default, it shows the names of the files and directories in alphabetical order. However, you can use various options to change the way it displays the information, such as showing hidden files, file sizes, permissions, owners, and more.

How to sort the output of ls by last modified date?

To sort the output of ls by last modified date, you need to use the -t option. This option sorts the files and directories by their modification time, with the newest ones first. For example, if you want to see the files and directories in your current working directory sorted by last modified date, you can run:

ls -t

This will show something like this:

file4.txt  file3.txt  file2.txt  file1.txt  dir2  dir1

In this example, file4.txt is the most recently modified file, followed by file3.txt, file2.txt, and file1.txt. The directories dir2 and dir1 are the least recently modified.

How to reverse the order of sorting by last modified date?

If you want to reverse the order of sorting by last modified date, you can use the -r option along with the -t option. The -r option reverses the order of sorting, so that the oldest files and directories are shown first. For example, if you want to see the files and directories in your current working directory sorted by last modified date in reverse order, you can run:

ls -tr

This will show something like this:

dir1  dir2  file1.txt  file2.txt  file3.txt  file4.txt

In this example, dir1 is the oldest directory, followed by dir2, file1.txt, file2.txt, file3.txt, and file4.txt. The file file4.txt is the most recently modified.

How to show more details about the files and directories?

If you want to show more details about the files and directories, such as their size, permissions, owner, group, and modification time, you can use the -l option along with the -t option. The -l option shows a long listing format with more information. For example, if you want to see the files and directories in your current working directory sorted by last modified date with more details, you can run:

ls -lt

This will show something like this:

-rw-r--r-- 1 user user   12 Jun 23 10:15 file4.txt
-rw-r--r-- 1 user user   11 Jun 23 10:14 file3.txt
-rw-r--r-- 1 user user   10 Jun 23 10:13 file2.txt
-rw-r--r-- 1 user user    9 Jun 23 10:12 file1.txt
drwxr-xr-x 2 user user 4096 Jun 23 10:11 dir2
drwxr-xr-x 2 user user 4096 Jun 23 10:10 dir1

In this example, each line shows a file or directory name along with its permissions (-rw-r–r– for files and drwxr-xr-x for directories), owner (user), group (user), size (in bytes), modification time (Jun 23 10:15), and name (file4.txt). You can see that the output is sorted by modification time from newest to oldest.

How to sort only files or only directories by last modified date?

If you want to sort only files or only directories by last modified date, you can use a combination of options and commands. For example, if you want to sort only files in your current working directory by last modified date, you can run:

ls -tp | grep -v /

This command uses two options: -t for sorting by modification time and -p for appending a slash (/) to directory names. Then, it pipes (|) the output to the grep command, which filters out (-v) any lines that contain a slash (/). The result is a list of files sorted by last modified date.

Similarly, if you want to sort only directories in your current working directory by last modified date, you can run:

ls -tp | grep /

This command uses the same options as before, but instead of filtering out lines that contain a slash, it filters in (grep) only those lines. The result is a list of directories sorted by last modified date.

Frequently Asked Questions (FAQs)

Question: How to sort files by creation date in Linux?

Answer: Linux does not store the creation date of files and directories, so you cannot sort them by creation date. However, some file systems, such as ext4, store the birth time of files and directories, which is the time when they were first created or copied. You can use the -U option with the ls command to sort files and directories by their birth time. For example:

ls -Ut

This command will sort files and directories by their birth time from newest to oldest. Note that this option may not work on some file systems or operating systems that do not support birth time.

Question: How to sort files by size in Linux?

Answer: You can use the -S option with the ls command to sort files and directories by their size. For example:

ls -S

This command will sort files and directories by their size from largest to smallest. You can also use the -r option to reverse the order of sorting. For example:

ls -Sr

This command will sort files and directories by their size from smallest to largest.

Question: How to sort files by name in Linux?

Answer: You can use the -X option with the ls command to sort files and directories by their name. For example:

ls -X

This command will sort files and directories by their name in alphabetical order. You can also use the -r option to reverse the order of sorting. For example:

ls -Xr

This command will sort files and directories by their name in reverse alphabetical order.

Summary

In this article, we learned how to sort files and directories by last modified date in Linux using the ls command and some options. We also learned how to show more details about the files and directories, how to sort only files or only directories, how to sort by other criteria such as size or name, and how to answer some common questions related to sorting in Linux.

Disclaimer: This article is for informational purposes only and does not constitute professional advice. The author and publisher are not responsible for any errors or omissions, or for any consequences arising from the use of the information in this article. Always consult a qualified professional before making any decisions based on the information in this article.