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!

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

16 Comments

Leave a Reply
    • @Prabhat,

      Thanks for appreciating us and our work, we will continue to impress you all with our quality articles, stay tuned to Tecmint..

      Reply
  1. Question 6 is either showing the wrong answer or the question doesn’t fully match the answer or maybe it was a problem translating the question to English.
    ls -R will display all files and directories recursively.
    ls -d */ will display only directories in the current directory.
    ls -d Pictures/*/ would display only directories in Pictures but not recursively.
    R and d can not be combined. Use “tree -d” or “find . -type d -print” instead.
    Another error in the same answer is that you imply that ls -Rm would list the directories separated by a comma. In reality only the files are comma separated.

    Reply
  2. On point “3. Well if switch (-h) output size in power of 1024, that is standard, What else power values are supported in ls command?” there is only one dash. I think it should be twice.

    Reply
  3. very useful information about ls command. Thanks for sharing this info. Actually i’m newbie to Linux. You gave all the commands with clear example that helps me alot. Once again thank you….

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.