Although it may only be two letters long, but one certainly can not underestimate the power of the ls command. Today, we are going to focus on understanding the output of the ls command, some useful options that can be used in conjunction with it and much more.
The ‘ls’ command is used to list the contents of a directory. It is your go to command if you wish to do some exploration and gather information about the files or other directories present inside a directory.
ls command with no option
Simply typing in the ls command with no option will list the contents of the current working directory in bare format with no details provided.
In case you wish to view the contents of a particular directory, then type in the ls command followed by the path to the directory which you wish to explore. It works in the following manner:
ls command with -l option
Using the ls command with the -l option displays the output in a long listing format. We usually use it in order to get a detailed information such as size, name of the owner, permissions and last modification date and time.
Here is what each field in this long listing format corresponds to:
Number of Links: 4
Owner Name: navanshu
Group Name: navanshu
Number of Bytes in the File: 4096
Last Modification Time: Jul 2 19:22
File Name: Desktop
In order to display the output with the size in a more convenient and human readable form, we can utilise the ls -lh command.
ls command with -a option
In Linux, files or directories beginning with a period (.), are considered to be hidden and are not displayed by default. In order to view them, we use the ls command with the -a option.
Bonus Tip: You can even use two options together in the same command. Suppose you wish to combine the -l and -a options, you could do so by typing in: ‘ls -l -a’ or ‘ls -la’ or even, ‘ls -al’ (the order is insignificant as long as the syntax is correct).
ls command with -F option
When we use the ls command with the -F option, a character is appended to the end of the file name which indicates its type.
Here is what each character indicates:
*– Executable Program
@– Link ( can be thought of as a shortcut to a file or directory)
ls command with -t option
Use the ls command with this option in case you wish to sort the output in accordance with the time at which it was last modified.
In the example given above, I have used the -t option in conjunction with the -l option, in order to make it clear how the -t option works. The command would work just fine even if we don’t mention the -l option.
ls command with -r option
The -r option comes in handy when we wish to output the reverse order while sorting.
ls command with -R option
Using the -R option causes the files and directories to be displayed recursively. This means that this command enables one to list the contents of the current directory as well as the contents of the directories placed within itself.
Bonus Tip: For a more visually appealing output one can also use the ‘tree‘ command to see the directory structure.
ls command with -1 option
Using the ls command with the -1 option restricts the output to a single entry per line.
This is only a small list of the vast variety of options available at your disposal while using the ls command. I would recommend you to check out the man page of the ls command to get to know about even more options that may turn out to be of some use.
Got any doubts, queries of your own, or any suggestions on the topics that you would like us to cover? Drop them in the comments below.