Linux Lexicon: Understanding The ‘ls’ Command In Linux

Tweet
Share
WhatsApp

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.

Like the cd command , the ‘ls’ command is also one of the most frequently used commands that newbies get their hands dirty with at the beginning of their Linux journey.

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.

ls command

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-withpath

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.

ls command-l_option

Here is what each field in this long listing format corresponds to:

Permissions: drwxr-xr-x
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-lh_option

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.

ls command-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-combination

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.

ls command-F_option

Here is what each character indicates:

/– Directory
*– 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.

ls command-t_option

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-r_command

Also Read: 20 Quirky Things You Didn’t Know The Linux Terminal Could Do

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.

ls command-R_option

Bonus Tip: For a more visually appealing output one can also use the ‘tree‘ command to see the directory structure.

ls command-tree_command

ls command with -1 option

Using the ls command with the -1 option restricts the output to a single entry per line.

ls command-1_option

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.

Read Our Whole Linux Lexicon Tutorial Series Here

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.

Navanshu Agarwal

Navanshu Agarwal

Ready to blow the world with crazy ideas.
More From Fossbytes

Latest On Fossbytes

Find your dream job