howtouselinux

3 ways to sort by time in Linux ls command

Table of Contents

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 human-readable format
  • ls -lrht #the most recently modified files display last, and output in long and human-readable format

 

Understanding ls command in Linux

The ls command is one of the most basic commands in Linux. It prints the contents of a directory to the screen. By default, it prints the names of all of the files and directories in the directory, but you can use various flags to display more information.

You can use the ls command to list the contents of a directory. For example, if you want to list the contents of your home directory, you would type: ls ~

ls command sort by time with -lht options in Linux

The most efficient way to sort by time in Linux ls command is using ls with -lht options. Open the terminal and type ls -lht. The most recently modified file will be listed on the first. The output is in the long format and the file size is in human readable format.

  • -l (The lowercase letter “ell”.) List files in the long format. It will give detailed information of files in columnar format.
  • -h option will show you the file sizes in human readable format. Size of the file is very difficult to read when displayed in terms of byte. The (ls -lh)command will give you the data in terms of Mb, Gb, Tb, etc.
  • -t option will sort files by the modification time

example:

% ls -lht|more
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.

 

ls command sort by time with -lt options in Linux

To sort by time in Linux ls command, we can use the -lt option. These two options will cause ls to sort files by modified time, with the most recently modified files appearing first and output is in long format.

  • -t Sort by descending time modified (most recently modified first). If two files have the same modification timestamp, sort their names in ascending lexicographical order.
  • -l (The lowercase letter “ell”.) List files in the long format. It will give detailed information of files in columnar format.

example:
#ls -lt|more
total 3469376
-rw-r–r–@ 1 howtouselinux staff 15575 4 4 13:31 compresspng.zip
-rw-r–r–@ 1 howtouselinux staff 15349 4 4 13:30 google-linux-min.png
-rw-r–r–@ 1 howtouselinux staff 72719 4 3 17:45 howtouselinux.zip

 

ls command sort by time with -lrht option in Linux

The -r option in ls command will reverse the order of the sort. This option will display files and directories in reverse order. By default “ls” command sort by name that is files or directories name.

Example:

ls -lrhs
-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

How to use ls command in Linux?

The list command (ls) is used at the command prompt of Unix and Linux systems.

  • To sort by file size, 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.
  • If you want to see more information about the files, you can use the -l option. This will show you the file size, permissions, owner, group, and modification time.

ls command options in Linux

  • You can use the . symbol to represent the current directory. For example, if you want to list the contents of the current directory, you can just type: ls .
  • You can use the -a flag with the ls command to show hidden files. Hidden files are typically configuration files that start with a . (dot). For example, if you want to list all of the files in the current directory, including hidden files, you would type: ls -a
  •  You can use the -l flag with the ls command to show more information about each file, such as the permissions, owner, group, size, and modification time. For example, if you want to list all of the files in the current directory with more information, you would type: ls -l
  • You can use the -R flag with the ls command to recursively list the contents of all subdirectories. For example, if you want to list all of the files in the current directory and all subdirectories, you would type: ls -R
  • You can use multiple flags at the same time. For example, if you want to list all of the files in the current directory, including hidden files, with more information, you would type: ls -a -l
  • You can use the > symbol to redirect the output of a command to a file. For example, if you want to list all of the files in the current directory and save the output to a file called list.txt, you would type: ls > list.txt
  • You can use the >> symbol to append the output of a command to a file. For example, if you want to list all of the files in the current directory and append the output to a file called list.txt, you would type: ls >> list.txt
  • You can use the | (pipe) symbol to redirect the output of one command into another command. For example, if you want to list all of the files in the current directory and then sort them by size, you would type: ls -l | sort -n

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!

 

 

 

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.