0% found this document useful (0 votes)
0 views9 pages

UNIX - Questions and Practice Exercises

This document provides a comprehensive guide to UNIX basics, file manipulation, the Vi editor, and shell programming. It includes commands, exercises, and explanations for various tasks such as listing files, changing permissions, and scripting. The content is structured into sections covering different aspects of UNIX usage and commands.

Uploaded by

divyadara63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views9 pages

UNIX - Questions and Practice Exercises

This document provides a comprehensive guide to UNIX basics, file manipulation, the Vi editor, and shell programming. It includes commands, exercises, and explanations for various tasks such as listing files, changing permissions, and scripting. The content is structured into sections covering different aspects of UNIX usage and commands.

Uploaded by

divyadara63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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

Which of the above files would qualify for following commands:


a) ls a?
b) ls a*
c) ls *.*
d) ls [!abc]art
e) ls [a!bc]art
f) ls [b-dku-z]*

 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]*

 The permissions 746 can be represented as which option below:


a) rwxrwx—x
b) rw—w-r-x
c) rwxr—rw-
d) rwxr-xr-x

 Create a file ‘temp’ that has below 3 lines:

$cat temp
Sita
Ram
Lakshman
What is the difference in output for:
wc –l temp
wc –l < temp

 What is the difference between hard link and symbolic/soft link?


o If original file is removed, hard link is not affected whereas softlink is useless now.
 How do you change the permissions of a file?
o chmod
 How will you get the number of lines/characters/words from a file?
o wc –l
o wc –c
o wc -w
 How will you select particular number of lines from starting of the file?
o head <filename>
 How will you select particular number of lines from end of the file?
o tail <filename>
 How will you find out the files which were created 90 days before?
o find –type f –mtime -90
 How will you remove the files which were modified 90 days back?
o find –type f –mtime -90|xargs rm -f
 How will you find out the files which were created by a particular user?
o find –type f –uname sk26761
 What is the difference between find and grep commands?
o Find will find the files
o grep will search within the files for a string and list the files that have the string
 How will you remove duplicate lines from a file?
o uniq
 How will you remove blank lines from a file?
o cat <filename>|grep –v “^$”
 How do you convert all uppercase letters to lower case in a file?
o tr
 I have a string, “UNIX is a very good language”. From this I would like to remove the characters
‘U’ and ‘g’ characters. So my output should be “NIX is a very ood lanuae”. How will you I achieve
this requirement?
o echo “UNIX is a very good language”|sed ‘s/[U,g]//g’
 What is /dev/null in UNIX?
o trash
3. Vi Editor
 Create a file called vi_practise.txt that has below contents:

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.

Also included here is a discussion on how attributes of a file can be manipulated.

Unix has quite a large number of commands, all of which are effective, efficient and perform
tasks perfectly.

Now open file in vi and do below:

 Add line numbers


 Change all occurrences of Unix to UNIX
 Join line numbers 2,3
 Delete second line
 Undo the previous deletion
 Remove line numbers
 Enter your name at the last line of file
 Save and come out

 What are the 3 modes of operation of vi editor?


 How to combine two consecutive lines in vi editor?
o J
 In vi editor, how to:
a) Move cursor to beginning of line
o ^
b) Move cursor to end of line
o $
c) Move cursor to beginning of first line of file
o :1
d) Move cursor to beginning of last line of file
o :$
e) Move cursor to beginning of 10th line of file
o :10

 In vi editor, how will you change:


a) All occurrences of ‘the’ to ‘THE’
o :%s/the/THE/g
b) All occurrences of ‘the’ or ‘The’ to ‘THE’
o :%s/[tT]he/THE/g
4. Shell Programming
 What does metacharater $# represent in shell scripting?
o Number of parameter values being passed to the script

 What will be output of below echo:


A=”*”
B=A
echo $B

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

 What will be output of below:


X=3
Y=3.5
if [ $X –eq $Y ]
then
echo X and Y are equal
else
echo X and Y are not equal
fi

o X and Y are not equal

 Any integer is input through the keyboard. Write a small script to find out whether it is an odd
number or even number

 What will be output of below program:

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 $?

You might also like