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

GREP OR: Match Any Of Multiple Patterns

GREP OR: Match Any Of Multiple Patterns-Find all the lines of a file, that match any of provided patterns.

We can use grep and egrep commands:

Syntex:

grep "PATTERN1\|PATTERN2" FILE
grep -E "PATTERN1|PATTERN2" FILE
grep -e PATTERN1 -e PATTERN2 FILE
egrep "PATTERN1|PATTERN2" FILE

Lets see it with the help of examples one by one:

I will use a log file to demonstrate above command:

abc.log is having pattern as below:

———————————————————————————————–suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………
khozbikar………………………33333……………………


1. Grep command with single search pattern:

Command:  grep 1111 abc.log

output: techsarthi……………..1111………………………(it will print the line having search pattern)

2.Grep command with multiple search pattern:

Command: grep “1111\|2222” abc.log

output: (above command will print all the lines having pattern 1111 or 2222)

suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………

3. Grep command with multiple search pattern:

Command:  grep -E “1111|2222” abc.log

output:  (above command will print all the lines having pattern 1111 or 2222)

suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………

4.Grep command with multiple search pattern:

Command:  grep -e 1111 -e 2222 abc.log

output:  (above command will print all the lines having pattern 1111 or 2222)

suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………

5. egrep command with multiple search pattern:

Command: egrep “1111|2222” abc.log

output:  (above command will print all the lines having pattern 1111 or 2222)

suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………

6.Grep command with multiple search pattern:

Command:  grep -E “1111|2222|3333” abc.log

output:  (above command will print all the lines having pattern 1111 or 2222 or 3333)

suman……………..22222………………….cbhvfvbbmbdidufg
techsarthi……………..1111………………………
khozbikar………………………33333……………………

UNIX GREP command in depth

TechSarthi- unix-ls-Comman-ls -lrt

GREP Command
GREP stands for Globally search a regular expression and print

1.Search a string and print from a log/text file

Ex- grep “this” abc.text

Above command will search for “this” string in abc.text and it will print the corresponding line to the console.

2. Case insensitive search

grep -I “this” abc.tx
Above command will search for “this/THIS” string in abc.text and it will print the corresponding line to
the console.

3. Regular expression:
Grep “line.*empty” abc.*

Above command will search for “line. Some text which ends with empty” string in all the file named as abc and
it will print the corresponding line to the console.

Will search for strings like: line.abcempty, line.notempty etc.

4.Full word search :

Grep -w “sum” abc.text
Above command will search for complete word “sum” in abc.text and it will print the corresponding line to the
console.

5. Full word search with regular expression:
Grep -w “sum” *.log
Above command will search for complete word “sum” in all the log files and it will print the corresponding line
to the console.