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

Akshat
UX/UI Designer at - Adobe

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...Read more

TL;DR

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

  1. -t: Sort by modification time (newest first)
  2. -r: Reverse the order of the sort
  3. -c: Sort by ctime (time of last status change)
  4. -u: Sort by access time (newest first)

For more complex file sorting, you can use Awk or Perl commands with ls. While the other way is to use options with ls includes -X to sort by file extension, -v for sorting by version number, and -s for sorting by size. To further enhance file sorting efficiency, you can use the other options like -l for detailed information, -R for recursive subdirectory display, and -1 for one file per line.

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

Sorting files by date and time is a common task for Linux users, particularly for managing log files and locating recently modified files. It helps to quickly find and manage files. To do this, you can use the ls command in Linux, as it provides a variety of options to sort files. So, in this article, I’ll cover the different options and techniques for sorting files with the ls command, along with some best practices for using its flags for efficient file management in Linux.

How to Sort Files with ls Command in 4 Basic Ways

To sort files with the ls command, you can use -lt for the last modification time (most recent first), -ltr for the oldest modifications first, -lc for the last change time of metadata, and -ltU for the last access time. The ls command provides a range of options for file sorting in Linux. 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

To Sum Up

Sorting ls command output by date and time is an essential skill for any Linux user who needs to manage files frequently. By using the right options and techniques, you can quickly locate and manage files more effectively. I hope this article has been helpful in providing you with a comprehensive overview of the different options and techniques to sort files using the ls command with its different options.

To improve your Linux skills for file management and organization, I recommend checking out my detailed guide on how to count files, unzip GZ files, and multiple ways to view file content in Linux. These articles will improve your Linux skills and empower you to navigate the system, automate tasks, and ensure file security.

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.

AkshatUX/UI Designer at - Adobe

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.

Total
0
Shares
Related Posts