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.
” .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:
3.ls -1 : Display one file per line. You can see the result without -1 and with, in below screenshot.
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.
5. .ls -ld : Display Directory information.
6. ls -lt : Order file based on last modified time.
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.
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.
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.
10. ls -F : Add special character for the classification.
/ – for directory
… – nothing for normal file
@ – link file
* – executable file
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.
12. ls -lS: Sort file by size, For more clarity we can try ls -lSh(h is for displaying size human readable format)
13. ls -lu Sort file by access time.
14. ls -t | head 1 : Display last file edited.