10 Useful ‘ls’ Command Interview Questions – Part 2

Continuing the legacy of ls command here is the second interview article on Listing command. The first article of the series was highly appreciated by the Tecmint Community. If you’ve missed the first part...

Spread the love

Continuing the legacy of ls command here is the second interview article on Listing command. The first article of the series was highly appreciated by the Tecmint Community. If you’ve missed the first part of this series you may like to visit at:

  1. 15 Interview Questions on “ls” Command – Part 1

This article is well presented in the manner that it gives deep insight of ls command with examples. We have taken extra care in the making of article so that it remains simple to understand yet serve the purpose to the fullest.

ls Command Interview Questions

10 Interview Questions on ls Command

1. You are supposed to list the content of a directory in long list format, but not print the name of the author and group the file belongs. Also show the difference in output.

a. ls command listing the name of the files in long listing format when used with switch (-l).

# ls -l

List Files in- Long List Format

List Files in- Long List Format

b. ls command listing the name of the files in long listing format along with the name of the author file belongs, when used with switch (–author) along with switch (-l).

# ls -l --author

List Files By Author

List Files By Author


c. ls command listing the name of the files without the name of its owner, when used with switch (-g).

# ls -g

List Files Without Printing Owner Name

List Files Without Printing Owner Name

d. ls command listing the name of files in long listing format without the name of group it belongs, when used with switch (-G) along with switch (-l).

# ls -Gl

List Files Without Printing Group

List Files Without Printing Group

2. Print the size of files and folders in the current directory, in human readable format. How will you do this?

Well we need to use switch -h (human-readable) along with switch (-l) and/or (-s) with the command ls to get the desired output.

# ls -hl

List Files in Human Readable Format

List Files in Human Readable Format

# ls -hs

List File Sizes in Long List Format

List File Sizes in Long List Format

Note: The option -h uses the power of 1024 (standard in computation) and output the size of files and folders in the units of K, M and G.

3. Well if switch (-h) output size in power of 1024, that is standard, What else power values are supported in ls command?

There exist a switch -si which is similar to switch -h. The only difference is switch -si uses power of 1000 unlike switch -h which uses the power of 1024.

# ls -si

Supported Power Values of ls Command

Supported Power Values of ls Command

It can also be used with switch -l to output the size of folder in the power of 1000, in long listing format.

# ls -si -l

List Files by Power Values

List Files by Power Values

4. You are supposed to print the contents of a directory separated with comma. Is it possible? Can it be done in long listing format?

Yup! Linux ls command can output the contents of a directory separated by comma when used with the switch (-m). Since this comma separated entries are filled horizontally, ls command can’t separate contents with comma when listing contents vertically.

# ls -m

Print Contents of Directory by Comma

Print Contents of Directory by Comma

When used in long listing format, switch -m gets useless.

# ls -ml

Listing Content Horizontally

Listing Content Horizontally

5. If there any way to print the contents of a directory upside down? i.e., in reverse order.

Yeah! The above situation can easily be achieved using the switch -r. The switch ‘-r‘ reverse the order of output. It can also be used with switch -l (long listing format).

# ls -r

List Content in Reverse Order

List Content in Reverse Order

# ls -rl

Long List Content in Reverse Order

Long List Content in Reverse Order

6. You are given a situation to print the sub-directories recursively. How will you achieve this situation? Mind it only sub directories and no files.

Okay! That is pretty easy with the switch -R when used with command ls. It can further be grouped with other options like -l (long list) and -m (comma separated), etc.

# ls -R

Print Sub Directories in Recursively

Print Sub Directories in Recursively

7. How to sort the files based upon the size?

The Linux command line option -S when used with ls gives the desired output. To sort the files based upon size in descending order with the largest file listed at first and smallest at last.

# ls -S

Sort Files in Linux

Sort Files with ls Command

To sort the files based upon size in descending order with the smallest file listed at first and largest at last.

# ls -Sr

Sort Files in Descending Order

Sort Files in Descending Order

8. List the contents of a directory with no additional information appearing one file per line.

The switch -1 comes to rescue here. ls command with switch -1 output the contents of the directory with one file per line and no additional information.

# ls -1

List Files Without Information

List Files Without Information

9. You are given a situation in which you have to print the contents of a directory on standard output enclosed by double quotes. How will you do this?

There exist an option -Q (quote-name) which output the content of ls enclosed in double quotes.

# ls -Q

Print Files with Double Quotes

Print Files with Double Quotes

10. You are working in a directory which contains lots of files and folders. You need to print the name of folders before directories. How will you get this?
# ls --group-directories-first

Print Directories First

Print Directories First

That’s all for now. We will be coming up with the next part of this article series about Quirky ‘ls’ Command Tricks. Till then stay tuned and connected to Tecmint. Do not forget to provide us with valuable feedback in the comment section below. Like and share us and help us get spread!

Facebook Comments

More Stuff

How to Find Out Postfix Mail Server Version in Linux by Ravi Saive | Published: November 11, 2017 | Last Updated: November 10, 2017Postfix is a popular, easy-to-configure and secure mail system that r...
The Complete 2018 CompTIA Certification Training Bundle (14 Course... Today, IT success stories start with the CompTIA certifications and here is The Complete 2018 CompTIA Certification Training Bundle, the ultimate (14 ...
How to Install X-Cart Shopping Cart in Linux X-Cart is a commercial open source e-commerce CMS platform written in PHP used for creating online stores for businesses and sell products. In this to...
Configuration of Zone Minder on Debian 9 In an earlier article, the installation of the security monitoring system Zone Minder on Debian 9 was covered. The next step in getting Zone Minder wo...
Spread the love

Posted by News Monkey