Interview Questions: - (UNIX - 3)
UNIX: -
Q) How to print lines without blank lines in a file?
Ans - $ grep –v ‘^$’ <file_name>
Q) Write a command to remove the first number on line 10 in file?
Ans - $ sed '10s/[0-9][0-9]*//' < filename
Q) Write a command to remove the first number on all lines
that start with "@"?
Ans - $ sed '\,^@, s/[0-9][0-9]*//' < filename
Q) Write a command to replace the word "apple"
with "(apple)" in a file?
Ans - $ sed s/apple/(&)/ < filename
Q) Write a command to replace the character '/' with ',' in
a file?
Ans - $ sed 's/\//,/' < filename
Q) Write a command to see your current directory you are present in unix system?
Ans - $ pwd
pwd – Present Working Directory
Q) Which command will show the creation, modification and access time, inode number and number of links of a file?
Ans - $ stat <File Name>
Q) Whether the file file.sh is present in /var/logs/script directory or not ?
Ans - $ find /var/logs/script –type f –name file.sh
Whereas
–type f -> option search only files
-name -> option related to name of any files or directory
If you will get the file name in output, then file is present in the above directory otherwise not.
Q) Give me those files which are modified between 5-7 days in current directory?
Ans - $ find . –type f –mtime -7 –mtime +5
-mtime -> modified time option in days
-ctime -> creation time option in days
-7 -> within 7 days
+5 -> beyond or before 5 days.
Q) Find those files whose size is 12 GB in current directory?
Ans - $ find . –type f –size 20g
Q) Find those files whose size is more than 20 GB in current directory?
Ans - $ find . –type f –size +20g
Q) Find those files whose size is less than 20 GB in current directory?
Ans - $ find . –type f –size -20g
Q) Find all files whose permission is 644 and change it to 755 in current directory?
Ans - $ find . –type f –perm 644 –exec chmod 755 {} \;
Q) How to delete the temporary files in /tmp directory?
Ans - $ find /tmp –type f –name ‘*.tmp’ –exec rm –f {} \;
Q) Find all the files created before 6 months having ‘core’ word in the name of the file and remove it ?
Ans - $ find . –type f –ctime +180 –name ‘*core*’ –delete
Or
$ find . –type f –ctime +180 –name ‘*core*’ –exec rm –f {} \;
Q) Search all ‘log’ files in /log directory and give me those files having ‘error’ word in it?
Ans - $ find /log –type f –name ‘*.log’ –exec grep –l error {} \;
Q) Move all the files which are not modified within 6 months to /usr/archieve directory?
Ans - $ find . –type f –mtime +180 –exec mv {} /usr/archieve \;
No comments:
Post a Comment