Skip to Content

3 ways to sort by time in Linux ls command

If you’re a Linux user, you may have noticed that the ls command sorts files alphabetically by default.

This can be helpful if you want to quickly find a specific file, but what if you want to see the most recently modified files first?

In this article, we will show you how to sort the output of the ls command by time.

3 ways to sort by time in Linux ls command:

  • ls -lt #the most recently modified files display first, and output in long format
  • ls -lht #the most recently modified files display first, and output in long and file size in human-readable format
  • ls -lrht #the most recently modified files display last, and output in long and file size in human-readable format

 

Understanding ls command

The ls command in Linux is used to list files and directories within a specified directory or the current working directory.

It provides a simple way to view the contents of a directory and obtain information about files and directories, such as permissions, ownership, size, and timestamps.

The command’s basic syntax is:

ls [options] [directory]

Here are some common options used with the ls command:

  • l: Long format listing, providing detailed information about files and directories, including permissions, ownership, size, modification time, and more.
  • a: Display all files, including hidden files that start with a dot (e.g., .bashrc).
  • h: Print file sizes in a human-readable format, using units like KB, MB, GB, etc.
  • t: Sort files by modification time, with the most recently modified files listed first.
  • r: Reverse the order of the sorting.
  • R: Recursively list subdirectories and their contents.
  • i: Print the inode number of each file.
  • -color: Enable colored output to distinguish file types and permissions.

 

sort by time in ls command

The most efficient way to sort by time in ls command is using ls with -lt options.

  • -l (The lowercase letter “ell”.) List files in the long format. It will give detailed information of files in columnar format.
  • -t option will sort files by the modification time

 

You can follow these steps:

  1. Open a terminal: Launch the terminal on your Linux system. You can usually find it in the applications menu or use the keyboard shortcut (e.g., Ctrl+Alt+T).
  2. Navigate to the directory: Use the cd command to navigate to the directory you want to list and sort.
    For example, to navigate to the “Documents” directory in your home folder:
    cd ~/Documents
    
  3. Use the ls command with the lt option: This will display the detailed information about each file and directory, sorted by their modification time, with the most recently modified files listed first.
    ls -lt
    

 

example:

% ls -lt
total 3469376
-rw-r--r--@ 1 howtouselinux staff 15K 4 4 13:31 compresspng.zip
-rw-r--r--@ 1 howtouselinux staff 15K 4 4 13:30 google-linux-min.png
-rw-r--r--@ 1 howtouselinux staff 71K 4 3 17:45 howtouselinux.zip

The following is the definition of columns.

 

sort by time with reverse order in ls command

The -r option in ls command will reverse the order of the sort. we can use the -lt and -r options to sort the files by modification time and display them in reverse order.

  • User enters ls -ltr command.
  • The system checks the options provided.
  • The -lt option tells the system to sort the files by modification time.
  • The -r option tells the system to reverse the order of the files.
  • The system lists the contents of the current directory or the directory specified, sorted by modification time in reverse order.
  • The output is displayed to the user.

 

Example:

ls -lrt
-rw-r--r--@ 1 howtouselinux staff 132M 1 18 12:44 Wireshark 3.6.1 Intel 64.dmg
-rw-r--r--@ 1 howtouselinux staff 170M 11 4 15:34 pgadmin4-6.1.dmg
-rw-r--r--@ 1 howtouselinux staff 480M 8 29 2021 pycharm-community-2021.2.1.dmg

 

Combine more options in ls command

You can combine multiple options in the ls command to modify its output. By combining options, you can get the exact output you need in a single command, rather than running multiple commands.

Options like -l (long format) and -h (human-readable sizes) provide detailed information about each file or directory, which can be very useful for administrative tasks or troubleshooting.

ls -lth

The above command will display the list of files and directories in the current directory, sorted by their modification time in long format, with the most recently modified files listed first.

Additionally, file sizes will be displayed in a human-readable format (e.g., KB, MB, GB).

FAQ ls command in Linux

Can the ls command list files in subdirectories as well?

Yes, the ls command can list files in subdirectories too. Use the -R option to recursively list subdirectories and their contents.

For example, ls -R will show files in the current directory and its subdirectories.

How do I get help on the ls command?

To get help and see the available options for the ls command, use the man command followed by ls. For example, man ls will display the manual page for the ls command, providing detailed information about its usage and options.

how to List files in the /var/log directory, including subdirectories and their contents:

ls -R /var/log

how to List the ten most recently modified files in the current directory:

ls -lt | head -n 10

how to List only directories in the current directory:

ls -d */

how to List files in the /usr/bin directory, displaying only the file names, and save the output to a text file:

ls -l /usr/bin > file_list.txt

how to sort by file size in ls command

you need to use the -S option. This will show the files in order from largest to smallest. You can also use the -r option to reverse the order and show the smallest files first.

 

Tips about Linux command

  • To find out more about a particular command, you can use the man command. For example, to find out more about the ls command, you would type: man ls
  • You can use wildcards to match multiple files. For example, the * wildcard matches any number of characters, so if you want to list all of the files in the current directory that start with the letter a, you would type: ls a*
  • You can use the up and down arrow keys on your keyboard to scroll through previous commands. This can be handy if you want to repeat a command or edit it slightly.
  • You can use the tab key to auto-complete file and directory names. For example, if you type ls /ho and then press the tab key, the system will automatically fill in the rest of the home directory for you.

 

We hope this article has helped you sort the output of the ls command by time. If you have any questions or comments, please let us know in the comment section below. Thank you for reading!