Interview questions on UNIX

List all links in current directory:
ls -lrt | grep "^l"
Create read only file
touch file
chmod 400 file

Find which operating system your system is running on in UNIX
uname -a
How to run a process in background?
Use “&” in the end of command
some_comand &
To bring to foreground use
fg JOBID
To get JOBID use
jobs
To kill use
kill -9 PID
To kill all processes in your system except the login shell
kill 0
Count number of lines in the file
wc -l file
Count number of characters in the file
wc -c file
How do you find which processes are using a particular file?
lsof
How to select column 3 and 6 in a CSV file with “,” as a delimiter
cut -f 3,6 -d "," file
Grep
How to count a number of non-empty lines in the file?
grep -cve '^\s*$'
In a file word UNIX is appearing many times? How will you count number?
grep -c “Unix” file
How do you find which process is taking how much CPU?
top
How do you check how much space left in current drive ?
df -h
How do you find how many cpu are in your system and there details?
cat /proc/cpuinfo
Sed
There is a file Unix_Test.txt which contains words Unix, how will you replace all Unix to UNIX?
sed s/Unix/UNIX/g file
Remove all blank lines in the file:
sed '/^$/d' people.csv
Remove all comments (assuming they start with #):
sed '/^#/d' people.csv
Split
Split datafile.csv into files with 1000 lines each, called
datafile.part.00, datafile.part.01 etc…
split -d -l 1000 datafile.csv datafile.part.
Find
How to list all cpp files modified less than 1 day ago
find . -name "*.cpp" -mtime -1
How to delete temporary files using find command in Unix?
find . -name "*.tmp" -print | xargs rm –f
How to find all text file which contains word Exception using find command in Unix ?
find . –name "*.txt" –print | xargs grep "Exception"
Managing directories
How will you find which directory is taking how much space?
du –sh . | grep G
How to synchronize directories with rsync via ssh:
rsync avz -e 'ssh -i privatekeyfile' /path/to/local/folder/ user2@server1:/path/to/local/folder
A good article on the use of rsync is here.

Managing programs
How to check software versions?
dpkg -l ipython

Advertisements
This entry was posted in Interview, Programming, UNIX/Linux and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s