UNIX Interview questions and answers Part -2

UNIX Interview questions and answers Part -2

UNIX Interview questions and answers Part -2 

1.       Grep

2.       Head

3.       ls

4.       lsof

5.       mkdir

6.       ps

7.       rm

8.       rmdir

9.       sort

10.   tail

11.   top

12.   touch

13. Vi editor

1.       GREP

Running the last executed grep command

This saves a lot of time if you are executing the same command again and again.

!grep

 

This displays the last executed grep command and also prints the result set of the command on the terminal.

2. Search for a string in a file
This is the basic usage of grep command. It searches for the given string in the specified file.

grep "Error" logfile.txt

 

This searches for the string "Error" in the log file and prints all the lines that has the word "Error".

3. Searching for a string in multiple files.

grep "string" file1 file2

grep "string" file_pattern

 

This is also the basic usage of the grep command. You can manually specify the list of files you want to search or you can specify a file pattern (use regular expressions) to search for.

4. Case insensitive search
The -i option enables to search for a string case insensitively in the give file. It matches the words like "UNIX", "Unix", "unix".

 

grep -i "UNix" file.txt


5. Specifying the search string as a regular expression pattern.

grep "^[0-9].*" file.txt

 

This will search for the lines which starts with a number. Regular expressions is huge topic and I am not covering it here. This example is just for providing the usage of regular expressions.

6. Checking for the whole words in a file.
By default, grep matches the given string/pattern even if it found as a substring in a file. The -w option to grep makes it match only the whole words.

 

grep -w "world" file.txt


7. Displaying the lines before the match.
Some times, if you are searching for an error in a log file; it is always good to know the lines around the error lines to know the cause of the error.

grep -B 2 "Error" file.txt

 

This will prints the matched lines along with the two lines before the matched lines.

 

 


8. Displaying the lines after the match.

grep -A 3 "Error" file.txt

This will display the matched lines along with the three lines after the matched lines.

9. Displaying the lines around the match

grep -C 5 "Error" file.txt

This will display the matched lines and also five lines before and after the matched lines.

10. Searching for a sting in all files recursively
You can search for a string in all the files under the current directory and sub-directories with the help -r option.

 

grep -r "string" *


11. Inverting the pattern match
You can display the lines that are not matched with the specified search sting pattern using the -v option.

grep -v "string" file.txt


12. Displaying the non-empty lines
You can remove the blank lines using the grep command.

 

grep -v "^$" file.txt


13. Displaying the count of number of matches.
We can find the number of lines that matches the given string/pattern

 

grep -c "sting" file.txt


14. Display the file names that matches the pattern.
We can just display the files that contains the given string/pattern.

 

grep -l "string" *


15. Display the file names that do not contain the pattern.
We can display the files which do not contain the matched string/pattern.

 

grep -L "string" *


16. Displaying only the matched pattern.
By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.

grep -o "string" file.txt



 

17. Displaying the line numbers.
We can make the grep command to display the position of the line which contains the matched string in a file using the -n option

 

grep -n "string" file.txt


18. Displaying the position of the matched string in the line
The -b option allows the grep command to display the character position of the matched string in a file.

 

grep -o -b "string" file.txt


19. Matching the lines that start with a string
The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.

 

grep "^start" file.txt


20. Matching the lines that end with a string
The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.

 

grep "end$" file.txt

 

2.       HEAD

It display first 10 lines of file by default

-          Head –n file1 à it display last n line from file

Ex. head -n 3 pass.txt

 

3.       TAIL

The tail command options are:

 

c : Prints the last N bytes of file; With leading +, prints the characters from the N byte in the file.

n : Prints last N lines; With leading + prints lines from the Nth line in the file.

f : Prints the appended lines on the terminal as the file grows.


Tail Command Examples
Create the following file in your linux or unix operating system for practising the examples:

 

File information in below

 

> cat example.txt

virtual storage

oracle virtual instance

mysql backup

dedicated hosting server

cloud servers


1. Display last 10 lines
> tail example.txt

By default, the tail command prints the last 10 lines from the file.


2. Display last N lines
Use the -n option to print the last n lines from the file. The following example prints the last 2 lines from the file:

 

> tail -n2 example.txt

dedicated hosting server

cloud servers


3. Print lines from the Nth line
You can print lines starting from the Nth line in a file. The following example prints lines from the 2nd line.

 

> tail -n+2 example.txt

oracle virtual instance

mysql backup

dedicated hosting server

cloud servers


4. Print the last n bytes.
use the -c option to print the last N bytes from the file. The following example prints the last 8 bytes from the file.

 

> tail -c8 example.txt

servers


5. Print characters from the Nth byte.
Use the leading "+" with -c option to print the characters from the Nth byte. The following example prints the characters from the 79th byte.

 

> tail -c+79 example.txt

cloud servers


6. Print last lines from dynamically changing file.
The -f option print the lines from file that is growing dynamically. When you run the tail -f filename command, it prints the last 10 lines and waits for new lines to be added to the file. Whenever the new lines are appended to the file, the tail command also appends the new lines on the standard output. The -f option is useful when debugging applications. In general, the applications writes error messages to log files. You can use the -f option to check for the error messages as and when they appear in the log file.

 

> tail -f logfile

 

 

4.       Ls

 

It display list of file and directories

-          Ls à list of files

-          Ls –a à display hidden files

-          Ls – lrt à Order Files Based on Last Modified Time (In Reverse Order)

-          Ls –lt à Order Files Based on Last Modified Time

-          Ls –d à Display Directory Informatio

 

5.       Lsof

 

List of open files

-          Lsof +D à List opened files under a directory

-          lsof -c ssh -c init à List opened files based on process names starting with

-          lsof -u username à list of files opened by specific user

-          lsof –p 11 à list of all open files by specific process id

 

6.       mkdir

it create directory

-          mkdir mydir à it create directory

-          mkdir -m a=rwx mydir à Create the mydir directory, and set its permissions such that all users may read, write, and execute the contents.

-          mkdir -p /home/chope/a/b/c à Creates the directory /home/chope/a/b/c. If the parent directory /home/chope/a/b does not already exist, mkdir will create that directory first

 

7.       rmdir

You can delete empty directory using rmdir command, or directory with content using rm command. Deletion can be done interactively, recursively, forcefully, or through alias

 

-          rmdir mydir à How to Delete Empty Directories

-          rmdir -p dir1/dir2/dir3 à Delete Nested Empty Directories

-          rm -rf DIRNAME à delete Directory with Files and Sub-directories

-          rm -f myfile.txt à Remove the file myfile.txt. You will not be prompted, even if the file is write-protected

-          rm -f * àRemove all files in the working directory. rm will not prompt you for any reason before deleting them.

-          rm -i * àAttempt to remove every file in the working directory, but prompt before each file to confirm.

-          rm -I * àRemove every file in the working directory; prompt for confirmation if more than three files are being deleted.

 

8.       Sort

Sort command is helpful to sort/order lines in text files. You can sort the data in text file and display the output on the scree

 

 

For example, here is a test file:

$ cat test

zzz

sss

qqq

aaa

BBB

ddd

AAA

 

-          Sort test

aaa

AAA

BBB

ddd

qqq

sss

zzz

-    sort -n test
11 qqq
22 zzz
33 sss
55 BBB

77 aaa

-          sort -M test à Sort Months of an Year using -M option
-          sort –c test à check content is already sorted or not
-          sort -k3 test

file

$ cat test

aa aa zz

aa aa ff

aa aa tt

aa aa kk

 

out put

$ sort -k3 test

aa aa ff

aa aa kk

aa aa tt

aa aa zz

 

17. TOUCH

It change the timestamp of file

-          touch file.txt à create an empty file

-          touch –c a.txt à use -c option to avoid creating new files. If you use -c option, and if a file doesn’t exists, touch will not create the file

-          touch a b c d à it will create more than one file at a time

-          touch –a a.txt à change the access time of a file using -a option. By default it will take the current system time and update the atime field

-          touch -m *.o à Change File’s Modification Time using -m

You can change the modification time of a file using -m option.The above method can be used to change the mtime of all obj files, when using make utility.

NOTE: It is not possible to change the ctime using touch command

 

touch -t [[CC]YY]MMDDhhmm[.SS]

Explicitly Setting Access and Modification time using -t and -d

Instead of taking the current time-stamp, you can explicitly specify the time using -t and -d options.

The format for specifying -t is [[CC]YY]MMDDhhmm[.SS]

 

The following explains the above format:

CC – Specifies the first two digits of the year

YY – Specifies the last two digits of the year. If the value of the YY is between 70 and 99, the value of the CC digits is assumed to be 19. If the value of the YY is between 00 and 37, the value of the CC digits is assumed to be 20. It is not possible to set the date beyond January 18, 2038.

MM – Specifies the month

DD – Specifies the date

hh – Specifies the hour

mm – Specifies the minute

SS – Specifies the seconds

 

For example:

 

touch -a -m -t 203801181205.09 tgs.txt

Verify the above change using stat command

Vi Editor

 

1.       vi  a.txt à open file with editable mode

2.       j -  move cursor down one line

3.       k - move cursor up one line

4.       h – move cursor left one charcter

5.       l – right move

6.       0 – move cursor to start of line

7.       $ - move cursor end of line

8.       w- move cursor to beginning of next word

9.       b – move cursor back to beginning preceding word

10.   i-  enter text

11.   dd – delete line

12.   d – delete the character

13.   :wq! – save file with quit

14.   :q! – quite file without saving

15.   ZZ – same as :wq!

16.   I – insert text beginning of line

17.   a- append text after cursor

18.   A – append text to end of current line

19.   o- open and put text in new line below current line

20.   O – open and put text in new line above current line

21.   Dw – delete word

 

How search word in vi editor file

 Click on esc button

Enter :

/ - string search forward for occurrence of string in text

: set nu/set number – display the line numbers

: n – here n is the line number – it will go to line number


Post a Comment

0 Comments