Sort Files with ls Command in Linux [4 Effective Methods]

Written by

Reviewed by

Last updated: July 24, 2024

Expert verified

SVG Image

TL;DR

To sort files with ls command in Linux, try some of the basic options listed here:

  1. Sort by Modification Time: Run ls -lt to list files with the newest modifications first.
  2. Reverse Sorting: Use ls -ltr to display files in reverse order, with the oldest modifications first.
  3. Sort by Change Time: Apply ls -lc to organize files by the last change to their metadata, showing the latest changes first.
  4. Sort by Access Time: Utilize ls -lu to arrange files by the last time they were accessed, highlighting the most recently accessed files.

Keep reading to find out how to sort files in Linux with the ls command, along with some best practices for using its flags.

Finding files in a messy directory can be a real hassle. Sorting your files can help you stay organized and save time. In this post, I’ll show you how to use the ls command in Linux to sort files easily. You’ll learn basic methods like sorting by time, size, and name, and advanced techniques using tools like awk and perl. By mastering these techniques, you’ll improve your file management efficiency and streamline your workflow. Let’s make file sorting in Linux simple and effective!

How Sorting Files in Linux Helps

Sorting files in Linux offers several benefits:

  1. Easier File Location: Sorting files makes it simpler to find what you need. For example, if you sort files by name, you can quickly locate a file alphabetically. Sorting by date lets you find the most recent files instantly.
  2. Better Organization: A sorted directory looks more organized. Whether you sort by name, size, date, or type, having a consistent order makes the directory easier to navigate.
  3. Efficient Data Management: Sorting helps you manage your data more effectively. For example, sorting by size helps you identify large files that might be taking up too much space. Sorting by date helps you track recent changes or additions.
  4. Simplified Backups: When you sort files, it’s easier to identify which files need backing up. Sorting by date can show you which files have changed since the last backup, making the process quicker and more efficient.
  5. Improved Performance: For directories with a large number of files, sorting can improve performance. For instance, if you regularly access the most recent files, sorting by date can save time and reduce the load on the system.
  6. Enhanced Collaboration: If multiple people access the same directory, a sorted file structure helps everyone find what they need quickly. This reduces confusion and improves team efficiency.

How to Sort Files with ls Command in 4 Basic Ways

To sort files with the ls command, start by running ls -lt to list files with the newest modifications first. For reverse order, execute ls -ltr to show the oldest modifications first. To organize files by their last metadata change, apply ls -lc. Lastly, utilize ls -lu to arrange files by their last access time, highlighting the most recently accessed files.

Here is the breakdown of these four methods that are commonly used with the ls command:

1. Sort by Modification Time (-lt)

The -t option is the most commonly used option for sorting files by modification time. It sorts the files in descending order based on their last modification time, with the newest files listed first. This option is useful for quickly locating recently modified files or tracking changes to a particular set of files over time. To sort files by modification time, follow these steps:

  1. In the command prompt, use the cd command to navigate to the directory containing the files you want to sort.
cd ~/directoryname
  1. Run the following command to sort the files by modification time (newest first).
ls -lt
  1. This command will list files and directories in the current directory, displaying detailed information about each entry and sorting them by the time of their last modification in descending order.
command to sort files by date and time

2. Reverse Sorting (-ltr)

The -r option is used to reverse the order of the sort. When used in combination with the -t option, it sorts the files by modification time but in ascending order, with the oldest files listed first. To sort files by modification time in reverse order, follow these steps:

  1. Head to the directory that contains the files you want to sort using the cd command in the Terminal app.
cd ~/directoryname
  1. Execute the command below to sort the files by modification time in reverse order (oldest first).
ls -ltr
  1. Now, this command will list files and directories in the current directory, displaying detailed information about each entry and sorting them by the time of their last modification in ascending order.
sort files by modification time in reverse order

3. Sort by Change Time (-lc)

The -c option is used to sort files by the time of the last change to their metadata, such as permissions or ownership. This option is useful for tracking changes to the metadata of important files. To sort files by change time, follow these steps:

  1. Use the cd command to navigate to the directory that contains the files you want to sort.
cd ~/directoryname
  1. Type the command below and press Enter to sort the files by ctime (time of last status change).
ls -lc
  1. This command will display detailed information about each entry and sort them by the time of their last change (or modification) in descending order. You’ll get the following output.
type ls lc command and press enter

4. Sort by Access Time (-lu)

The -u option is used to sort files by the time of the last access to their contents. This option is useful for tracking how frequently files are accessed and identifying which files are no longer in use. To sort files by access time, follow these steps:

  1. Launch the Terminal app and navigate to the directory where your files are located using the cd command.
cd ~/directoryname
  1. Execute the command below to sort the files by birth time (newest first).
ls -lU
  1. This command will sort the files by access time (newest first) in the Terminal view. However, this option is limited, so you may need to check your distro’s documentation to see if it’s available.
sort files by access time in the terminal view

How to Sort Files with ls Command Using 5 Advanced Methods

In addition to the options provided by the ls command, there are more advanced techniques you can use to sort files by date and time. Here are a couple of examples:

1. Sorting with Awk

Awk is a versatile command-line tool designed for text processing, which can be used effectively to sort ls command output by date and time. With its built-in scripting language and pattern-matching capabilities, Awk provides a range of options to manipulate and transform text data, making it a valuable resource for managing and organizing files. To use Awk for sorting with the ls command output by date and time, follow these steps:

  1. Change to the directory where the files you want to sort are located.
cd ~/directoryname
  1. Type the command below in the Terminal window.
ls -l | awk '{print $6 " " $7 " " $8 " " $9}' | sort -k 1M -k 2n
  1. Once you execute this command, it will list the files and their attributes in a long format due to -l, extract the date and time information from the ls output using awk, and sort the files by month (1M) and then by day (2n). The sorted output will be displayed in the Terminal app with the newest files listed first.
sorted output will be displayed in terminal app

2. Sorting with Perl

Perl, a highly versatile programming language, can be used effectively for sorting the ls command output by date and time. With its robust text processing capabilities and extensive libraries, Perl allows users to manipulate and transform text data, thereby streamlining file management tasks. Here’s how to use it:

  1. In the Linux command prompt, head to the directory where the files are located using the cd command.
cd ~/directoryname
  1. Type the command below in the Terminal app.
ls -l | perl -e 'print sort { (stat $a)[9] <=> (stat $b)[9] } <>'
  1. Press Enter to execute the command, and the output will show the files sorted by modification time (newest first).
output will show files sorted by modification time

3. Sorting by File Extension (-lX)

If you want to sort files by their file extension rather than by date and time, you can use the -X option. Here’s how to use it:

  1. Go to the directory containing the files you want to sort by executing the cd command in the command prompt.
cd ~/directoryname
  1. Execute the following command to sort the files by extension.
ls -lX
  1. This option sorts the files by extension alphabetically, with directories listed first.
option sorts files by extension alphabetically

4. Sorting by Version Number (-lv)

If you’re working with files that have version numbers in their names, you can use the -v option to sort them by version number. Here’s how to use it:

  1. Enter the cd command prompt to change the current directory to the one containing the files you want to sort.
cd ~/directoryname
  1. Run the following command to sort the files by version number (ascending).
ls -lv
  1. Now, you’ll see that this option sorts the files by their version numbers in ascending order in your Terminal window.
sorts files by their version numbers in ascending order

5. Sorting by Size (-ls)

In addition to sorting files by date and time, you can also sort files by size using the -s option. This option sorts files by size in descending order, with the largest files first. To use the -s option, follow these steps:

  1. In the Terminal window, navigate to the directory with the files you want to sort.
cd ~/directoryname
  1. Execute the following command to sort the files by size (largest first).
ls -ls
  1. Once you run this command, you’ll see your files sorted based on largest to smallest size in your Terminal screen.
sort the files by size largest first

3 Best Practices to Use ls Command with Options

To use ls command with options for file sorting output, it’s essential to be familiar with a few tips and tricks. Having this knowledge will enable you to utilize the command’s options more effectively and achieve better file organization. Here are some valuable tips and tricks to enhance your experience with the ls command:

1. Displaying Detailed Information (-l)

When you include the -l flag with the ls command, it provides a detailed listing of files, including permissions, owner, group, size, and timestamps. This can be useful when you want to examine various attributes of the files alongside their timestamps.

display detailed information about files

2. Recursive Listing (-R)

If you want to list files in subdirectories as well, you can use the -R flag with the ls command. This option recursively displays files in all subdirectories, allowing you to see the entire directory structure and sort files by date and time within each subdirectory.

display files in subdirectories recursively

3. One File Per Line (-1)

If you prefer a more compact and easily readable output, you can use the -1 flag. This option displays each file on a separate line, which can be especially helpful when dealing with a large number of files or when you want to quickly scan through the list.

display one file per line for easier readability

Final Thoughts

Sorting files can make it easier to find what you need, manage data better, and improve system performance. I have also discussed methods like sorting by modification time, change time, access time, and reverse order, along with advanced techniques using tools like awk and perl.

If you found this guide helpful, consider reading more about:

Frequently Asked Questions

Can I sort files by creation time on all Linux systems?

No, sorting files by creation time is not universally possible using the ls command on all Linux systems. Creation time (ctime) is not consistently recorded or accessible in the file metadata. Some file systems may store this information, but accessing and sorting by creation time requires specialized tools or commands. Alternative commands like stat or file managers may offer sorting options based on creation time. Availability and accessibility of creation time can vary based on the file system, system configuration, and Linux distro.

Is it possible to sort files using a graphical file manager on Linux?

Yes, graphical file managers on Linux, such as Nautilus, Dolphin, Nemo, and Thunar, provide options to sort files by various criteria like name, size, type, and date modified. Simply clicking on the column headers allows you to sort files in ascending or descending order based on the selected criterion.

How can I sort only certain types of files using the ls command?

To sort only certain types of files by date and time using the ls command, use the -t flag, along with a file name pattern that matches the desired file type. For example, to sort text files, use ls -lt *.txt. This will list only the specified file type, sorted by their modification timestamps.

Is it possible to sort files based on a specific time range?

To sort files by date and time within a specific time range, use the find command with the -mtime option. For example, to find files modified in the last 24 hours and sort them by modification time, use find . -mtime -1 -type f -exec ls -lt {} + to adjust the -mtime value to specify a different time range, like -7 for the last week or -30 for the last month. You can also customize the ls command with options like -S for file size or -r for reverse sorting by name.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

[5 Proven Fixes] “ValueError: could not convert string to float”

Next Post

[6 Proven Fixes] “zipimport.zipimporterror: can’t decompress data; zlib not available”

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next