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

The listing command in UNIX and UNIX like operating System ‘ls‘ is one of the most basic and widely used utility in command-line. It is POSIX compliant utility available for GNU coreutils and BSD variants.

The ‘ls’ command can be used with a variety of options to get desired results. This article aims at deep insight of file listing command in Linux with relevant examples.

ls Command Questions
15 ls Command Questions
1. How will you List files from a directory?

Ans: The Linux file listing command ‘ls‘ comes to rescue here.

# ls
List Files in Linux
List Files

Alternatively, we can use ‘echo’ command to list files within a directory in association with wildcard (*).

# echo *
List All Files in Linux
List All Files
2. How will you list all the directories only using echo command?
# echo */
List All Directories in Linux
List All Directories
3. How will you list all the files within a directory including hidden files aka (.) dot files?

Ans: We need to use option ‘-a‘ (list hidden files) with command ‘ls‘.

# ls -a
List All Hidden Files in Linux
List All Hidden Files
4. How do you list all the files within a directory including hidden files, but do not list implied ‘.’ and ‘..’?

Ans: We need to use option ‘-A‘ (do not list implied . and ..) with command ‘ls‘.

# ls -A
Do Not List Implied
Do Not List Implied
5. How will you print the content of a directory in long format listing?

Ans: We need to use option ‘l‘ (long format) with command ‘ls‘.

# ls -l
List Files Long
List Files Long

In the above example, the output seems like.

drwxr-xr-x  5 avi tecmint      4096 Sep 30 11:31 Binary

Here, drwxr-xr-x is file permission for owner, group and world. Owner has Read(r), Write(w) and Execute(x) permission. The group to which this file belongs has Read(r) and Execute(x) permission but not Write(w) permission, same permission implies for the world that have access to this file.

  1. The Initial ‘d‘ means its a Directory.
  2. Number ‘5‘ represents Symbolic Link.
  3. The File Binary belongs to user avi and group tecmint.
  4. Sep 30 11:31 represents the date and time it was last modified.
6. You are supposed to print the content of directory in long format listing, showing hidden/dot files. How will you achieve this?

Ans: We need to use option ‘-a‘ (list hidden files) and ‘-l‘ (long listing) together with command ‘ls‘.

# ls -la
Print Content of Directory
Print Content of Directory

Alternatively We can use option ‘-A‘ and ‘-l‘ with ‘ls‘ command, if we do not want to list implied ‘.’ and ‘..’.

# ls -lA
7. How will you figure out the author of each file?

Ans: We need to use option ‘–author‘ along with option ‘-l‘ to print the author name of each file.

# ls --author -l
List Author Files
List Author Files
8. How will you print escape for non-graphic character?

Ans: We just need to use option ‘-b‘ to print escape for non-graphic character.

# ls -b
Print Escape Character
Print Escape Character
9. List the size of files and folders in desired scale format. How will you achieve this?

Ans: Here option ‘–block-size=scale‘ along with option ‘-l‘ needs to be used. We need to remove ‘scale’ in the example with the desired scale viz M, K, etc.

# ls --block-size=M -l
# ls --block-size=K -l
List File Scale Format
List File Scale Format
10. List the files within a directory, but don’t show the backup files, i.e., those files that end with ~.

Ans: Here option ‘-B‘ (do not list implied entries ending with ~) comes to rescue.

# ls -B
List File Without Backup
List File Without Backup
11. Sort all the files within a directory by name and show associated last modification information.

Ans: We need to use option ‘-c‘ and option ‘-l‘ with command ls to fulfil the need as suggested above.

# ls -cl
Sort Files in Linux
Sort Files
12. Sort all the files within a directory by modification time and show associated information.

Ans: We need to use three options together i.e., ‘-l‘, ‘-t‘ and ‘-c‘ with command ls to sort files by modification time, newest first.

# ls -ltc
Sort Files by Modification
Sort Files by Modification
13. How will you control the output of ‘ls’ command to be colorful or no-color?

Ans: We need to use option ‘–color=parameter‘. The parameter to be used with color option are ‘auto’, ‘always’ and ‘never’ which are self explanatory.

# ls --color=never
# ls --color=auto
# ls --color=always
ls Colorful Output
ls Colorful Output
14. You are supposed to list directory entries themselves, not their contents. What will you do?

Ans: Here the option ‘-d‘ comes handy.

# ls -d
List Directory Entries
List Directory Entries
15. Create an alias for long format listing “ls -l” as “ll” and output the result to a file and not standard output.

Ans: Here in the above scenario, we need to add alias to .bashrc file and then use redirect operator to write the output to file and not standard output. We will be using editor nano.

# ls -a
# nano .bashrc
# ll >> ll.txt
# nano ll.txt
Create Alias for ls command
Create Alias for ls command

That’s all for now. Don’t forget to provide us with your valuable feedback in the comments below. I’ll be here again with another interesting article soon. Till then stay tuned and connected.

Real Also:

  1. 10 ‘ls’ Command Interview Questions – Part 2
  2. 15 Basic ‘ls’ Commands in Linux
If you read this far, tweet to the author to show them you care. Tweet a thanks
Ronav Saive
A Passionate GNU/Linux Enthusiast and 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.

27 thoughts on “15 Interview Questions on Linux “ls” Command – Part 1”

  1. Thanks for this tutorial

    About `ls -d`, when is mandatory or useful? it only returns "." at a first glance has no sense, could you update the answer to question #14? Thanks in advance

    Reply
  2. Excuse me? What is the point of these interview questions, please? What position would I be hiring for and ask myself – “has this candidate memorized all possible options of the ls command?”. These are great questions for the geek pub quiz but not job interviews…

    Reply
  3. In-correct bit of information: on Question #5, the 2nd bullet point says that “Number ‘5‘ represents Symbolic Link.”

    (a) The numeric field(2) between permission & file owner is the LINK_COUNT, but only includes direct/hard links.

    (b) Symbolic links do NOT change/increment the link count for a regular file or directory.

    (c) Unix/Linux allows the same exact file to have multiple names.

    1. These are not copies of file, but exact same file can be called red and blue at the same time,
    2. and both names will be listed in directory ‘ls’ listing… the names can be in different DIRs also.

    (d) Link_Count shows count for “How Many simultaneous hard-link Names does this File HAVE?”

    1. You can also have “Symbolic or ‘soft’ links; but those are re-direction pointers and not counted the same.”

    (e) Rule: You are NOT allowed to add hard-links (extra names) for a Directory… just normal files and programs.

    (f) For a DIR, both the “dir-name” in parent dir, and the “.” dot-name in child dir are part of Link_Count,

    1. so Dirs have Link-Count 2 when created… All “normal files” have a Link_Count of 1 when created.

    (e) Link count on Dirs goes UP as sub-dirs are created, since “..” entry in sub-dirs is a link pointing to parent dir.

    (f) So… if you see a DIR with Link_Count above 2, you can subtract 2, and know number of child sub-dirs

    (g) Thus, in Question#2 above, the Link_Count=5 means that dir “Binary” contains three sub-dirs.

    1. but the Link_Count does not tell you if there are Symbolic_Links, as noted in the example answer.
    Reply
  4. I do like the contents of the post, as I learned new things. But if a company was interviewing me from man pages, I’d run screaming and never look back, no matter how good the pay they’d offer. I do value my sanity, you know.

    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.