UNIX - Questions and Practice Exercises
UNIX - Questions and Practice Exercises
1. UNIX Basics
Print today’s date in YYYYMMDD format
o date “%Y%m%d”
Create a command called “today” that will print today’s date in YYYYMMDD format. So at
command line if I type – today, I should get today’s date in YYYYMMDD format
o alias today=’date “%Y%m%d”’
Change your password successfully
o passwd
How do you list hidden files?
o ls -a
How will you find out the which version of Unix you are working and the processor on which this
version is running?
o uname –a or uname -v
What does PID and PPID stand for?
o PID is Process ID
o PPID is the Parent Process ID (PID for process that is the parent of this process)
How will you run a process in background?
o Put & at the end.. Eg. ls –lrt *.dat &
How will you remove a directory which is an empty and which is not an empty directory?
o Empty directory : rmdir <directory_name>
o Non-Empty directory : rm –rf <directory_name>
How will you know in which directory you are currently working in?
o pwd
How will you change your directory to the last working directory?
Example: you have worked in /root/siva and from there you changed your directory to
/root/scott/home/dev. Now again I would like to go to /root/siva.
o cd -
How do you find out your hostname?
o hostname
How will you know the release/version of your host?
o uname -a
How do you know what are all the current running processes in the server?
o ps -e
How do you know the jobs which are running in background?
o jobs
How do you run a job in background? What is the significance of running jobs in background?
o Long running commands are run in background so the command line is available for
other commands
o Put & at the end of the command
How do you bring a background job into the foreground?
o fg
What is the difference between different quotes in UNIX (“ ” and ‘ ’ and ` `)
o “ ” Parameter value is printed
o ‘ ’ Parameter name is printed
o `` Command is executed
Eg.
$ echo "$HOME"
/tmp/sk26761
$ echo '$HOME'
$HOME
$ echo `ls -lrt *UAL.txt`
-rw-r--r-- 1 sk26761 abinitio 145 Jan 18 02:35 20150116_UAL.txt
How do you empty a file without removing the file?
o echo “” > abcd.txt
How will I redirect my output to both standard output and as well as to a file?
o tee
From a directory get the top 5 files which are consuming more space.
o ls –ls |head -5
How can I display only the records from 50 to 55?
o cat <file_name>|head -55|tail -5
How will you select the characters from 3 to 7 from the 3rd line in file?
o cat <file_name>|head -3|tail -1|cut –c3-7
2. Playing with Files
Suppose there are following files present in current directory:
art
part
part1
part2
part3
mozart
tart
quartz
How will you list all the files in the current directory whose second character is a digit?
o ls –lrt ?[0-9]*
How will you list all the filenames starting with ‘a’ or ‘b’ or ‘k’?
o ls –lrt [a,b,k]*
$cat temp
Sita
Ram
Lakshman
What is the difference in output for:
wc –l temp
wc –l < temp
Unix files do have number of attributes associated with each one of them. A list of all these
attributes along with their meanings has been discussed here.
Unix has quite a large number of commands, all of which are effective, efficient and perform
tasks perfectly.
o A
Write a small script that will take input from user keyboard entry for length and breadth and
calculate the area of the rectangle:
$ksh area_calculator.ksh
Enter Length of Rectangle:
12 User Input
Enter Breadth:
10 User Input
AREA of Rectangle is 120
Any integer is input through the keyboard. Write a small script to find out whether it is an odd
number or even number
suite=3
case $suite in
1) echo Diamond ;;
2) echo Spade ;;
3) echo Heart ;;
*) echo Not a Valid suite ;;
esac
o Heart
Write a shell script that when executed, gives output depending on current time:
“Good Morning” if time between 5am to 12 noon
“Good Afternoon” if time between 12noon to 3pm
“Good Evening” if time between 3pm to 8pm
“Sleep Please” if time between 8pm to 5am
What will be the first line of the shell script and what is the significance of that?
o First line is
#! /usr/bin
o Significance is that this path gives the location for all shell commands
How do you find out the number of arguments passed to a script?
o $#
How do you find out the status of the last command execution?
o $?