Tuesday, May 22, 2007

Basic Unix Commands & how to...

If you are going to appear in an interveiw requiring some basic unix knowledge. Following will be helpful for revision.



1)How to use find command to find a file in a directory and its child directories.

find –name -print

2) How will u change extensions of file names (of one pattern to another pattern… eg.. changing *.c to *.cpp in a directory)
Script:

for file in `ls *.ccut –d . –f1`
do
mv $file.c $file.cpp
done


3) What is the command to list only the sub directories in a directory?
ll grep “^d”

4) What are hard and soft link?

In the case of a hard link an inode entry with same inode number is created. When the parent or child file is removed only the no. of links is reduced.
In the case of soft link if the parent file is deleted the linked file will not be able to access the main files’ the data.

4) What are file permissions?
Read , write and execute


5) How will u change the owner of a file?
chown

6) How will u initiate a background process?
By using & at the end of the command

7) How will you suspend a process?
To suspend a foreground process, type control-Z. To suspend a background process, you must know its job number. For example, to suspend background job 2, type:
% stop %2
8) If you are given a task to run which takes several hours to complete and your terminal login kicks you out for more than half an hour of inactivity. What will you do?

use nohup to start the task.


9)What in nohup?
nohup is used to run a command and continue running it in the background even when you logout .
nohup does not automatically put the command it runs in the background; you must do that explicitly, by ending the command line with an `&'.

Syntax : nohup &

10) How will u see for a particular process and how will u kill it?(using process id)
ps command is used to find the process id of a process
kill –9 kills the process
NOTE: If the process you are trying to kill has dependent child processes then, don't use the -9 option in kill command. -9 kills the job forcefully and doesn't let it wait until all the dependent child processes are killed.

11) I have a directory and I have files in it. Write a shell script which will rename all the files starting with letter “F” with letter “G”
ls -l F* awk '{print "mv "$NF" "$NF}' sed -e 's/^F/G/2' /bin/sh
12) What is nice command?
If no arguments are given, nice prints the current scheduling priority, which it inherited. Otherwise, nice runs the given command with its scheduling priority adjusted. If no adjustment is given, the priority of the command is incremented by 10. The superuser can specify a negative adjustment. The priority can be adjusted by nice over the range of -20 (the highest
priority) to 19 (the lowest).
Syntax: nice [-n adjustment] [-adjustment]

13) What is a pipe in Unix?
Pipe ( ) is a mechanism by which the output of one command/executable is redirected as input of other command. This is a single directional pipe and goes from left to right in a unix command.

No comments: