Use
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.
Syntax
ls [OPTION]… [FILE]…
Example of 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.
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.
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
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)
8. Identify directories explicitly
-p: prints “/” character after the directories
So the “/” helps to identify the directory immediately.
9. Reverse sort the output
-r: prints the output in reverse alphabetical order
10. List the directories recursively
-R: option displays the directory contents in recursive manner.
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
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