ls Command in Linux | List Files and Directories

In Linux, all the file/directory management operations can be handled through the commands and the “ls” command utility lists the files and directories available at a specific path. This utility offers various options to get the details of the files/directories.  In this blog, information about the “ls“ command and its usage with different examples will be explained, and the post’s content is as follows:

Let’s start this!

What is the ls Command Utility in Linux?

The “ls” command utility in Linux lists down the information of the content available in the specified directory. The usage of the “ls” command is provided below:

Syntax:

$ ls [Options] [Directory]

Some of the basic options are provided in the following table:

-aThis option will display the hidden contents of the directory
-lThis option will print the author’s information of the directory
~The information of the home directory contents is displayed by using this option
-lhThis option will display the information in human-readable format
–versionThe installed version of the ls command can be displayed using this option
How to install Kali Linux in Window...

To explore more details on the “ls” utility, use the following command in terminal.

$ ls --help

For more understanding of the ls command, some examples are used in the next sections.

What is the Usage of the ls Command in Linux?

The usage of the ls command in Linux has been explained by executing different options of the ls command in different examples.

Example 1: List the Content of a Current Directory

To list down all the directories/files of the current directory, we use the “ls” command without using any of its options as follows:

$ ls

All the contents of the home directory have been displayed.

Example 2: List the Content With its Permissions (Long Listing the Content)

Different directories and files are assigned different permissions, including the read, write, and execute permissions. To display the file’s permissions using the ls command, use the “-l” option:

$ ls -l

The permissions are displayed with the contents.

Example 3: List Content of a Directory in Human-Readable Format

If you want to display the information of the directories in the human-readable format, then use the “lh” option with the ls command:

$ ls -lh

The information has been displayed in a human-readable format. It just converts the space occupied in “Bytes”, “KBs”, “MBs”, etc., instead of only “Bytes,” which is the default unit of size in the “ls” command.

Example 4: List the Content of the Directory in Specific Unit Size

We can also display the size of the directories/files using the “–block-size” option with the measurement unit of the size. For example, if the directory size is supposed to display in the kilobyte, then the “K” should be used as

$ ls -l --block-size=K

The output shows that all the files/directories sizes are now in “KB’s”. Similarly, the unit’s value can be set to “M” (for megabyte), “G” (for gigabyte), and so on.

Example 5:  List the Content of the Parent and Home Directory

To display the content of the parent directory, use the “ls” command as follows:

$ ls ../

The content of the “Home” directory can be displayed using the “ls” command as follows:

$ ls ~

From the output, it can be seen that the terminal was not in the “Home” directory. However, the content of the “Home” directory is displayed using the command.

Example 6: Display the Hidden Files

The ls command utility can also display the hidden files using the “a” option. To display the hidden files, run the command:

$ ls -a

It displays all the files, and the file names starting with the full stop are hidden files in Linux.

Example 7: Display the Files by Using their Path Address

To display the files using the path address where the file is located is also possible. To display the files, use the complete path of the file in the command:

$ ls /home/itslinux/Downloads/

The contents of the Downloads Directory have been displayed with the “ls” command.

That’s all about the “ls” command tutorial

Conclusion

The “ls” command is used to display the information of the directories. It can also list the permissions of all the files in a directory. Using “ls”, the size of each file can also be checked and much more. This post has demonstrated the detailed usage of the “ls” command in Linux with the help of suitable examples.