I will share the convenient options for the ls command to sort the list of files. The ls command is for listing files and directories, but by default it sorts the list by name. Typically, the ls command displays directories mixed with files. Some implementations of the ls command support a useful option for sorting and display format. In this article, we will figure out how to properly structure files in the terminal workspace.
Let’s start by executing some basic ls commands.
Table of Contents
Running the ls command without adding any argument will display the contents of the current directory. In order to sort the list of files the following options are used
ls -l Command
You can also display detailed information about each file, for example, file permissions, number of links, owner name, file size, last modification time, etc. This can be achieved with the -l option, which means a long list format, as in the following screenshot:
$ ls -l
ls -lt Command
Finally, to list files in a directory and sort them by the last change in date and time, use the -t option, as in the command below:
$ ls -lt
Read more: How Download File with CURL?
ls -ltr Command
If you need to sort files from “newer” to “older” based on date and time, you can use the -r option to display as follows:
$ ls -ltr
That’s all, but the ls command has a lot more options, so try viewing them on the man page. This is important since every Linux or Mac user needs to know how to use the sort command.