ls Command Options in Linux With 10 Examples

Zaheer Ahmad
5 min readOct 9, 2021

The ls command utility list the information about the files in the current directory by default. This information includes Size, Creation Date, Permissions, Owner, and Group associated with the files and directories. The ls command sorts the output in alphabetical order by name.

In this guide, you will learn everything about the ls command with 20 examples. The ls is a simple and powerful command when working with a large amount of data/files. Especially, when processing files based on specific dates.

In many automation projects, I used the power of the ls command to get my job done. The ls command has many arguments that help us in formatting the output in the desired format.

We will explore all the arguments of the ls command in detail with examples in this article.

1. List Directory Contents

ls

When we type the ls command without any argument it lists the files/directories in the current directory without giving any information about the files.

$ ls

In the output, you can see all the files and directories present in our current directory. Here you can also see different colors.

Blue Color: Represents the Directories

White Color: Represents all the Text Files

Purple Color: Represents the Images

2. List All Directory Contents | ls -a

ls command with -a argument lists everything in the current directory including the hidden files and directories. When you want to see hidden files use ls with -a argument.

$ ls -a

The ( . ) symbol at the start of the files or directories represents the hidden files and directories.

If you look at the output in the above screenshot, you can see ( . ) and ( . . ) at the beginning. The single ( . ) symbol represents the current directory. The ( . . ) symbol represents the previous directory that is one level up to the current directory.

3. List Directory Contents of Previous Directory | ls ..

ls .. command lists the files and directories in the previous directory.

Example

When you open your terminal you are always in the home directory of the current user. When you open the terminal, you are in /home/username. You can verify that using the pwd command. In my case, the username is zaheer and I’m in /home/zaheer.

if you do ls it will list the files and directories present in /home/username.

if you do ls .. it will display files and directories present in /home directory.

$ ls ..

4. List Directory Contents With long Listing Format | ls -l

ls -l command list the files and the directories in long-listing format. The -l argument to ls command shows all the information like permissions, Owner, Group, Size, Creation Date, and Name of the files and the directories.

$ ls -l

5. List Directory Contents With Inode Information | ls -il

List the files and directories with inode information.

The argument -i is the inode. When given -i argument along with -l to the ls command it shows the inode information of each file and directory.

$ ls -il

6. List Directory Contents In Human Readable Format | ls -hl

The ls command has -h argument when given with -l or -s arguments shows the output in a human-readable format.

The human-readable format means it gives us information of files/directories sizes in 1K 234M 2G etc.

$ ls -hl

7. List Directory Contents of Specific Directory | ls [path-to-directory]

To list the contents of the specific directory type the directory name if in the current directory otherwise give the full path of the directory after the ls command

ls [Directory-Name]
ls [Path-To-Directory]
$ ls Desktop

OR

$ ls /home/username/Downloads

Replace the username with your actual username. In my case, zaheer is the username.

If you do not know the username you can find out your username by reading this article

8. List Content of Subdirectories Recursively | ls -R

Till now, we only listed the contents of the current directory or the directory we supply to the ls command.

But what about how to list the contents of all the directories in the current directory or our desired directory?

ls command has an argument of -R that gives us the ability to list the content of the desired directory recursively.

Recursively mean it will go to each directory that it will find one-by-one and will list its contents

ls -R will list the contents of the current directory recursively.

$ ls -R

ls -R [path-to-directory] will list the contents of the directory you want to list recursively.

$ ls -R Documents/

In the above screenshot, you can see the difference between the ls and ls -R commands.

9. ls Sort By Name In Ascending Order

By default, the ls command sorts the output by name alphabetically where numbers are given high priority than Capital letters and then small letters.

$ ls

10. ls Sort By Name in Descending Order

Using -r argument with ls sorts the output in descending order.

$ ls -lr

Questions For You

Check the permissions on the /etc/passwd file using the ls command

Check who is the Owner of /etc/shadow file using the ls command

Final Note

If this article is helpful for you. Please share it with your friends and comment about the article. If you have any suggestions you can post your comment and we will update this article with your suggestions.

useradd-command-in-linux-with-examples

Password Aging Information and Configuration in Linux

/etc/passwd file in Linux. Learn How To View and Read it.

/etc/shadow File Fields in Linux.

--

--

Zaheer Ahmad

I am a DevOps Engineer. Working as a freelancer for 3-years. I love Linux. I am an expert in writing bash scripts to automate the task.