Professional Documents
Culture Documents
Unix Interview
Unix Interview
This file maintains each and every user information with 7 fields. The 7 fields are
Username:passwd:uid:gid:fullname:home:shell.
What is Shell?
Shell is a command line interpreter. Shell acts as an interface between user and the
kernel.
What is Kernel?
Kernel is core part of unix o/s. It is a group of hundreds of system calls.
What are different flavours of unix o/s?
Any operating system designed based on unix kernel called as flavour of unix. The
following are some flavours of unix
Linux ---- Red Hat
Sun solaris --- Sun Microsystem
IBM-AIX ----- IBM
Hp-ux ----- HP
Sco-unix ----- santa crus operations
IRIX------ Silicon Graphics.
1. Password protection.
2. File permissions.
3. Encryption.
ls -r lists the files in reverse alphabetical order... whereas ls -R lists the files and
directories recursively
How do you search for a string in a directory with the subdirectories recursed?
grep -r string *
Difference between grep, egrep and fgrep
egrep : accepts more than one pattern for search. Also accepts patterns from a file.
fgrep : accepts multiple patterns both from command line and file but does not accept
regular expressions only strings. It is faster than the other two and should be used when
using fixed strings.
How to display 2 and 4 th fileds from a given file if the delimetr is “:”?
Cut –d”:” –f 2,4 filename
How to display unique lines from a given file?
Sort –u filename
How to eliminate completely duplicate lines from a given file?
Uniq –u filename
How to remove all duplicate lines from a file?
Uniq –u filename > tempfilename
Mv tempfilename filename
How to delete “hello” word from a given file?
Sed “s/hello//” filename
How to compare two files are same or not?
Cmp
How to display the first 10 lines from a file?
Head -10 filename
Write a one line command to convert all the capital letters of a file "test" into lower
case?
cat filename | tr "[A-Z]" "[a-z]"
The pipeline to list the five largest files in the current directory is
The pipeline to find out the number of times the character ? occurs in the file is
tr -dc '?' < file | wc -c ( Delete all the characters except ? and then make a word count.)
Write allows you to create, edit and delete files and subdirectories in the directory.
Execute gives you the previous read/write permissions plus allows you to change into the
directory and execute programs or shells from the directory.
if there is a process u want to run even after exiting the shell what is the
command used?
Nohup
which command will get executed even after you log out?
Nohup
What is the command to send message to all users who are logged in?
Wall
The other way of running shell script apart from using sh command and
chmod?
19. How do you refer to the arguments passed to a shell script? - $1, $2 and so on. $0 is
your script name.
20. What’s the conditional statement in shell scripting? - if {condition} then … fi
21. How do you do number comparison in shell scripts? - -eq, -ne, -lt, -le, -gt, -ge
22. How do you test for file properties in shell scripts? - -s filename tells you if the file is
not empty, -f filename tells you whether the argument is a file, and not a directory,
-d filename tests if the argument is a directory, and not a file, -w filename tests for
writeability, -r filename tests for readability, -x filename tests for executability
23. How do you do Boolean logic operators in shell scripting? - ! tests for logical not, -a
tests for logical and, and -o tests for logical or.
24. How do you find out the number of arguments passed to the shell script? - $#
25. What’s a way to do multilevel if-else’s in shell scripting? - if {condition} then
{statement} elif {condition} {statement} fi
26. How do you write a for loop in shell? - for {variable name} in {list} do {statement}
done
27. How do you write a while loop in shell? - while {condition} do {statement} done
28. How does a case statement look in shell scripts? - case {variable} in {possible-value-
1}) {statement};; {possible-value-2}) {statement};; esac
29. How do you read keyboard input in shell scripts? - read {variable-name}
30. How do you define a function in a shell script? - function-name() { #some code here
return }
31. How does getopts command work? - The parameters to your script can be passed as
-n 15 -x 20. Inside the script, you can iterate through the getopts array as while getopts
n:x option, and the variable $option contains the value of the entered option.
Command Action Example
alias Creates a temporary name for a Unix command. alias name command
To remove an alias: unalias name
ansiprint Prints a file to a printer connected to your computer. ansiprint file
assets Displays your account resources, use, and threshold.
bg Places a suspended job in the background.
cat Displays a specified file. cat file
cd Changes current directory to a different directory. cd directory
To change back to your home directory: cd
Directory abbreviations:
~ Home directory
.. Parent directory
. Current working directory
chmod Changes permission status for a file or directory. chmod code file
chmod code directory
To keep a file private: chmod 600 file
To give everyone read permission: chmod 664 file
compress Reduces the size of a file and adds .Z to the file's name. compress file
To restore a compressed file: uncompress file.Z
cp Makes a copy of a file. cp file1 file2
To copy a file into a different directory: cp file directory
To inquire before overwriting an existing file: cp -i file1 file2
diff Compares two files and displays the differences.
fg Restarts a suspended job in the foreground.
finger Displays user information at the specified computer. finger userid@computer
To change your finger entry information: chfn
ftp Starts the file transfer program with a remote computer. ftp address
grep Finds lines in a file matching a character pattern. grep -icvn pattern file
-i Ignores case
-c Lists count of lines that contain pattern
-v Lists all lines except those with pattern
-n Lists line number for each found pattern
head Displays the first ten lines of a file. head file
To display the first n number of lines: head -n file
help Displays an online help note. help note
To display a list of help notes: help -l
history Displays a list of your most recent commands. history
To repeat the nth command from the list: ! n
To repeat your most recent command: !!
jobs Displays active jobs and their corresponding job numbers. jobs
To start a job in the background: job &
To return a job to the foreground by the job number: fg %number
To send a running job to the background: z bg
kill Terminates a process by its process identification number. kill pid
To use the strongest form of the kill command: kill -9 pid
logout Ends your computer session.
ls Lists the files and subdirectories in your current directory. ls -Fltra
-l Lists long format
-t Lists by modification time
-r Lists in reverse order
-F Lists file type with special character
-a Lists all files including dot (.) files
lynx Starts a text-only Web browser session lynx
Starts a Web browser session at a specific Web address. lynx url
man Displays Unix Manual entry for a command. man command
To search Manual Index for a keyword or topic: man -k keyword
mesg Displays your talk and write message access status. mesg
To set your message status to yes or no: mesg y (or n)
mkdir Creates a new directory within your current directory. mkdir directory
more Displays a file one screen at a time. more file
Use to scroll forward and q to quit.
mv Moves a file to a different directory. mv file directory
Moves a directory to a second directory. mv directory1 directory2
To rename file1 as file2: mv file1 file2
To rename directory1 as directory2: mv directory1 directory2
To inquire before overwriting an existing file: mv -i file1 file2
passwd Starts program to change account password.
pico Starts the Pico text editor with a file. pico file
pine Starts the Pine® electronic mail program.
ps Displays processes and their corresponding process
identification numbers.
pwd Displays absolute pathname of current directory.
rm Permanently deletes a file. rm file
To verify prior to removal: rm -i file
rmdir Deletes an empty directory. rmdir directory
To verify prior to removal: rmdir -i directory
spell Checks the spelling in a file. spell file
telnet Starts a telnet session with a remote computer. telnet address
vi Starts the vi text editor with a file. vi file
w Displays a list of who is on system.
wc Counts lines, words, and characters in a file. wc file
Redirection :
< Routes input to command from file
> Routes output from command to file
>> Appends output to existing file
| Routes output between commands
Control codes :
s Suspends the screen display
q Restarts suspended display
d Signals end of file
c Cancels a command
z Suspends a process or job:
use fg to restart
u Clears the command line
FILE COMMANDS:
cp Copy files.
Usage: cp [options] FileName1 File Name2
cp [options]
Options: -r recursively copy directory structures.
rm Remove files.
Usage: rm [options] filename
Options: -r recursively remove directory structures.
-i query user for confirmation.
more View file contents in sections determined by the size of the terminal.
Usage: more
less View file contents in sections determined by the size of the terminal.
Has more options and search features than more.
Usage: less [options]
compress Reduces the size of the file. and adds the extension .Z
Usage: compress .
SYMBOLIC LINKS
ln Create symbolic links between files or between directories.
Usage: ln [options]
ln [options]
Options: -s allows linking across file systems and allows the display
of the link’s name upon ls -l.
Eg: ln -s course-file myfile
Eg: ln -s course-directory myspace
TERMINAL COMMANDS
clear Clears the terminal.
HELP COMMANDS
man Displays the manual page for the selected command.
Usage: man
rtfm Displays the man page and help files ont he terminal.
Usage: rtfm
INFORMATION COMMANDS
history Lists the commands typed during the session.
Options: -r displays the list in reverse.
# How to check the command line arguments in a UNIX command in Shell Script?
In a bash shell, you can access the command line arguments using $0, $1, $2, …
variables, where $0 prints the command name, $1 prints the first input parameter of
the command, $2 the second input parameter of the command and so on.