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]…
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.
There are many options that are available with ls command. Here we will discuss the most common ones only.
-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.
-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.
-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.
-f: options shows the output without using the default colour scheme
-g: same as -l option but it does not print the name of the owner/user
-i: prints the inode number of the file
-n: same as -l option but instead of printing the user and group names, it prints the UserID (UID) and GroupID(GID)
-p: prints “/” character after the directories
So the “/” helps to identify the directory immediately.
-r: prints the output in reverse alphabetical order
-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.
-S: option sorts the output by file size. Hence, the output is sorted with the largest files listed first
-t: sorts the output based on file modification time
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