Unix Questions and Answers – Listing Directory Contents: Is Command

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Listing Directory Contents: Is Command”.

1. Which command is used for listing files in a directory?
a) ps
b) list
c) ls
d) wc
View Answer

Answer: c
Explanation: ls command is one of the most powerful commands used in UNIX system. It comes available with a bunch of options for listing files available in the directory. There are a number of options like -l, -a which are used for listing files in different manners according to user’s need.

$ ls
001.sh        
FILE2       
abc.txt
dir08

2. Which option is used with ls command for long listing of files with seven attributes?
a) -a
b) -l
c) -x
d) -i
View Answer

Answer: b
Explanation: -l is used with ls command for long listing of files in ASCII collating sequence. The attributes displayed are:
-> File type and permissions
-> Links
-> Ownership
-> Group ownership
-> File size
-> Last modification time
-> Filename

advertisement
advertisement
$ ls   -l
drwxr-xr-x     5 abd      users   4096   Aug 12  08:30 go
drwx------      3 mash    users   4096   Nov 28  10:49 irc
drwxr-xr-x    2 ryan      users   32768  July 25   09:15 logs
drwxr-xr-x    8 anki      users   4096    Oct  2   17:13 src

3. Which option is used with ls to display attributes of the directory ?
a) -a
b) -ld
c) -d
d) -o
View Answer

Answer: c
Explanation: ls command when used with directory name as an argument, lists all the files in the directory. To force ls to list the attributes of the directory we use the -ld option.

$ ls  -ld 
drwxr-xr-x     5 george   users   4096  Sep 25  08:30 go
drwx------      3 george   users   4096  Sep 27  10:49 irc
drwxr-xr-x    2 george   users   32768 Oct  4   09:15 logs
drwxr-xr-x    8 george   users   4096   Oct  2   17:13 src

advertisement

4. -a option is used with ls command for what purpose?
a) for showing hidden files
b) for showing executables
c) for showing directories
d) for multi columnar output
View Answer

Answer: a
Explanation: -a option when used with ls command displays all the files including hidden files. Files beginning with a ( . ) are listed using -a option. There are certain files (filenames beginning with a . ) These files are not listed without -a option.

advertisement
$ ls  -ax
./	. ./	calendar
.file	.rhost	abc_001

5. Which option is used with ls command to produce multi columnar output?
a) -x
b) -F
c) -a
d) -v
View Answer

Answer: a
Explanation: If there are multiple files present in a directory for listing, it is better that we display these files in multiple columns. For this purpose -x is used. For example,

$ ls  -x
File1        file2       abc.txt
Dir08       res.c       txt05
Dep.lst   emp.sh    udisk

6. What is the collating sequence of ls command for listing files?
a) Numerals > Uppercase > Lowercase
b) Numerals > Lowercase > Uppercase
c) Lowercase > Uppercase > Numerals
d) Uppercase > Numerals > Lowercase
View Answer

Answer: a
Explanation: The output of ls commad is arranged in ASCII collating sequence which is, filenames starting with Numerals first, then Uppercase, then lowercase.

$ ls 
001_dir                 //numeral first
09_abc            
ABD.txt                //uppercase
prog.sh		      //lowercase

7. How can we identify executables and directories from the output of ls command?
a) using -F option
b) using -x option
c) using -a option
d) using -i option
View Answer

Answer: a
Explanation: The output of the ls command simply displays filenames without indicating the file type. For identifying the file type we use -F option. The output of ls -F displays filenames with two symbols * and /, which are type indicators. The filenames containing * are executables while those containing / are directories.

$ ls  -Fx
008abc.txt       TOX.sh*     calendar*
Dept.lst           helpdir/     progs/

8. $ ls -R outputs all files and subdirectories in a recursive manner.
a) True
b) False
View Answer

Answer: a
Explanation: The above command traverses the directory tree in a recursive manner until no subdirectories are left. In simple words, we can say that it produces a recursive listing of files.

9. -r option is same as -R option in ls command.
a) True
b) False
View Answer

Answer: b
Explanation: Both -r and -R are different options and performs different tasks. -r is used to sort the filenames in reverse order while sorting while -R is used for recursive listing of files.

10. What does the -S option do in ls command?
a) sorts according to modification time
b) sorts according to last access time
c) sorts according to file size
d) sorts according to inode number
View Answer

Answer: c
Explanation: ls -S sorts the filenames according to the file size with largest size first.

drwxr-xr-x     5 abd      users    78988  Aug 12  08:30 go
drwx------      3 mash    users   56899   Nov 28  10:49 irc
drwxr-xr-x    2 ryan      users   32768  July 25   09:15 logs
drwxr-xr-x    8 anki      users    4096    Oct  2   17:13 src

11. ls -s prints the allocated file size in blocks.
a) True
b) False
View Answer

Answer: a
Explanation: Files are stored onto the disk in terms of block size. The block size allocated to the file can be displayed using -s option with ls command.

12. -u option sorts the files according to ____
a) last modification time
b) last access time
c) ASCII collating sequence
d) file size
View Answer

Answer: b
Explanation: ls -u displays the filenames based on the value of the last access time of the file i.e. it sorts all the filenames according to their last access time.

13. -t option sorts the files according to ____
a) last modification time
b) last access time
c) ASCII collating sequence
d) file size
View Answer

Answer: a
Explanation: ls -t displays the filenames based on the value of the last modification time of the file i.e. it sorts all the filenames in accordance with their last modification time.

14. The output of ls dir* is ___
a) all files in the current directory
b) all files in the directory having a filename starting with dir
c) no filename is displayed
d) erroneous
View Answer

Answer: b
Explanation: The above command displays those files in the directory, the names of which will be starting with a prefix ‘dir’. Here * is a meta-character which is used for pattern matching.

$ ls  dir*
dir01
dir02 
dir00.txt

Sanfoundry Global Education & Learning Series – Unix.

To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.