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.
- file size
- file type
- file permissions
- number of links to the file
- owner of the file
- group owner of the file
- size of the file in bytes
- last modified date / time of the file
- file or directory name
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.
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!