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 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!

If You Appreciate What We Do Here On Jassweb, You Should Consider:

Jassweb is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit Jassweb! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?