How to use ls command in Linux?


ls command in Linux is used to list the directory contents. If a directory name is not specified then the contents of the current working directory are displayed.


ls [OPTION]… [FILE]…

Example of How to use ls command in Linux?

how to use ls command in linux?

First, we use ls without specifying any directory name. So it displays the contents of the current working directory which is cse325. Next, we use ls by giving the directory name which is IPC. So, it displays the contents of IPC directory.

Using Options with ls command

There are many options that are available with ls command. Here we will discuss the most common ones only.

1. Displaying hidden files also.

-a: option displays all the files within a directory including the hidden files also.

How to use ls command in linux? with -a option

The output with -a option shows extra entries that start with a dot “.” For example .secret.

2. Displaying long-list

-l: option displays the long list of the files and sub-directories. The long list contains detailed information about the files and sub-directories.

How to use ls command in linux?

Hence, use the -l option to know extra information about the files like user name, group name, size etc.

3. List the directories itself

-d: By default the ls command displays the directory content. But what if you want to list the directory name itself or you want to display the long list about the directory and not its content? The answer is -d option.

4. Disable coloured output

-f: options shows the output without using the default colour scheme

5. Print long list without user name

-g: same as -l option but it does not print the name of the owner/user

6. Print the index number of the file

-i: prints the inode number of the file

how to use -i option with ls?

7. Print UID and GID

-n: same as -l option but instead of printing the user and group names, it prints the UserID (UID) and GroupID(GID)

how to use -n option with ls?

8. Identify directories explicitly

-p: prints “/” character after the directories

how to use -p option with ls?

So the “/” helps to identify the directory immediately.

9. Reverse sort the output

-r: prints the output in reverse alphabetical order

how to use -r option with ls?

10. List the directories recursively

-R: option displays the directory contents in recursive manner.

how to use -R option with ls?

With -R option the contents of current directory are displayed. Also, the content of subdirectories CPU_Scheduling, Disk_Scheduling, IPC are also displayed.

11. Sort by file size

-S: option sorts the output by file size. Hence, the output is sorted with the largest files listed first

how to use -S option with ls?

12. Sort by modification time

-t: sorts the output based on file modification time

Some helpful Questions on ls command in Linux

Q1. What is the output when the argument to ls command is a file name?
Q2. Can you list contents of two directories simultaneously using ls?
Q3. Consider a Directory D1 having two files f1 and f2. Write the command to
1. Print the details about the files f1 and f2
2. Print the detail about the directory D1
3. Know the inode number of only f1
4. Know the size of f2

PPT on ls command

Video on ls command

Relative commands


Leave a Reply