UNIX – ls command Tutorials, Interview questions and Explanations

In this post I will try explain all the “ls” command related concepts frequently asked in interview for support and testing. I have used unix server to give the example for all the concepts for better understanding, still you have any query feel free to ask in comment section.

1. ls -l : Display all the information about files/Directories, in below example we are displaying the information from UnixTutorials folder.

2. ls -a : Display all the files including hidden files.

unix-ls-Comman-ls -a, ls -a : Display all the files including hidden files.

” .node_repl_history” is the hidden file in current directory. ls -A can also be used to list hidden files, the difference has been highlighted below:

ls -a : Display all the files including hidden files

3.ls -1 : Display one file per line. You can see the result without -1 and with, in below screenshot.

unix-ls-Comman-ls -1 interview question

4. ls -lh : Display file size in human readable format. It will show the file size in K, KB, MB etc. You can see the difference in below example total is 148K in case when we are using ls -lh and its value is 148 in case ls -l.

unix-ls-Comman-ls -lh

5. .ls -ld : Display Directory information.

unix-ls-Comman-ls -lh

6. ls -lt : Order file based on last modified time.
unix-ls-Comman-ls -lt

7. ls -lrt : Order file based on last modified time in reverse order. For reference we can compare result of ls -lt(example 6) and ls -lrt.

TechSarthi- unix-ls-Comman-ls -lrt

8. ls -R : Display files Recursively or we can say all the folders, subfolders and files will be displayed. In below example folder a1 and b2 is having sub folder Tests1 and Test2 respectively.

TechSarthi

9. ls -n : Display file VID and GID in numeric format. You can see the difference between ls -n and ls -l in below example.

TeshSarthi ls -n : Display file VID and GID in numeric format

10. ls -F : Add special character for the classification.
/ – for directory
… – nothing for normal file
@ – link file
* – executable file

TechSarthi unix-ls-Comman-ls -F

11. ls – -color=auto: Classification of file using different color. In below example I only two types of file hence showing blue for folder and white for normal files.

TechSarthi unix-ls-Comman-. ls --color=auto

12. ls -lS: Sort file by size, For more clarity we can try ls -lSh(h is for displaying size human readable format)

tECHsARTHI unix-ls-Comman-. ls -lS

13. ls -lu Sort file by access time.

TechSarthi unix-ls-Comman-ls -lu

14. ls -t | head 1 : Display last file edited.

TechSarthi unix-ls-Comman- ls -t head 1

5 Replies to “UNIX – ls command Tutorials, Interview questions and Explanations”

Leave a Reply

Your email address will not be published. Required fields are marked *