Professional Documents
Culture Documents
Unix Hands On Examples
Unix Hands On Examples
Ex: Date +%m =12 , date +%h =Dec and date +”%h %m”
huxd0275:/home/p1216319/venkat# tty
/dev/pts/0
1) The HOME variable: the home directory: UNIX automatically places you in a directory called the
home directory.
UNIX Variable:
3) Cd : change directory
1) cat : displaying and creating files (Concatenates a set of files. Is also used to list a file)
3) cp: coping a file cp file1 file2 , cp file1 file2 file3 file4 pros
cp file1 /home/p1216319/file1
Coping directory structure : cp -R dir1 dir2
6) Mv : command to move a file into another directory. Also rename an existing file or directory
with the mv command
Syntax
mv source-filename destination-directory
mv oldDirectory newDirectory
5) head
displays the top lines of the file. By default prints top 10 lines. Has a numeric switch.
E.g.
huxd0275:/home/p1216319/venkat# head -n 3 emp.lst
2233|a.kshukla |g.m | sales |12/12/78|6000
9876|jai sharma |director|production|12/03/80|7000
5678|sumit chakrvarthy |d.g.m | marketing|19/05/75|6000
6) tail
displays the bottom lines of the file. By default prints bottom 10 lines.
E.g.
huxd0275:/home/p1216319/venkat# tail -3 emp.lst
3564|sudir |executive|personal|06/11/77|7500
2345|j.prassanna |g.m |marketing|31/12/76|8000
0110|v.k.sham |g.m |marketing|12/31/74|9000
This will display bottom 3 lines from the emp.lst file.
Cut: while head and tail splits a file horizontally, cut is used to slice a file vertically
cut identifies both columns and fields
Cutting columns: (-c)
huxd0275:/home/p1216319/venkat# cut -c 5-10 emp.lst
|a.ksh
|jai s
|sumit
|sudir
|j.pra
|v.k.s
Cutting Fields (-f):
huxd0275:/home/p1216319/venkat# cut -d \| -f 2,3 emp.lst
a.kshukla |g.m
jai sharma |director
sumit chakrvarthy |d.g.m
sudir |executive
j.prassanna |g.m
v.k.sham |g.m
The default delimiter between the columns in a paste is a tab, but options allow
other delimiters to be used.
8) Sort:
huxd0275:/home/p1216319/venkat# cat emp.lst
2233|a.kshukla |g.m | sales |12/12/78|6000
9876|jai sharma |director|production|12/03/80|7000
5678|sumit chakrvarthy |d.g.m | marketing|19/05/75|6000
3564|sudir |executive|personal|06/11/77|7500
2345|j.prassanna |g.m |marketing|31/12/76|8000
0110|v.k.sham |g.m |marketing|12/31/74|9000
huxd0275:/home/p1216319/venkat# sort emp.lst
0110|v.k.sham |g.m |marketing|12/31/74|9000
2233|a.kshukla |g.m | sales |12/12/78|6000
2345|j.prassanna |g.m |marketing|31/12/76|8000
3564|sudir |executive|personal|06/11/77|7500
5678|sumit chakrvarthy |d.g.m | marketing|19/05/75|6000
9876|jai sharma |director|production|12/03/80|7000
9) I/O redirection:
huxd0275:/home/p1216319/venkat# cat dept.lst >> dept.lst1
12) Finger
huxd0275:/home/p1216319/venkat# finger
Login Name TTY Idle When Site Info
p1216319 Venkatreddy Vennapus p0 Thu 09:22
huxd0275:/home/p1216319/venkat# script -a
Script command is started. The file is typescript.
$ script logfile
Script command is started. The file is logfile.
$ exit
Script command is complete. The file is logfile.
$
Script command is complete. The file is typescript.
Options
Script -a append to existing typescript file
Script logfile use <file> instead of default file typescript
13) tee
Sends standard input to specified files and also to standard out. It’s often used in
command pipelines. To view and save the output from a command.
15) man; If you have problem with command usage, use the man command
man command_name
Eg1: In this simple example, assume myfile is 3,000 lines long: split myfile
This will output four 40KB files: segmentaa, segmentab, segmentac, and segmentad.
if test -r CPPCPDWKLYorg.txt
then rm CPPCPDWKLYorg.txt
fi
Compares two sorted files, and compares each line of the first with each line of the second and
reports commonalties and difference between them.
It produces a 3 column output
first column show lines unique in the first file
2nd column show lines unique in the second file
3rd column show common lines in both files
Eg:
huxd0275:/home/p1216319/venkat# comm emp.lst emp.lst1
2233|a.kshukla |g.m | sales |12/12/78|6000
9876|jai sharma |director|production|12/03/80|7000
5678|sumit chakrvarthy |d.g.m | marketing|19/05/75|6000
3564|sudir |executive|personal|06/11/77|7500
2345|j.prassanna |g.m |marketing|31/12/76|8000
0110|v.k.sham |g.m |marketing|12/31/74|9000
5) diff :
compares two files, directories, etc, and reports all differences between the two.
It’s output format is designed to report the changes necessary to convert the first file into the
second.
Very popular program that works one or more filenames. It provides the extension .gz to the
compressed filename and removes the original file.
9) Reporting Free Space : Disk Df –k . : This will gives the disk space for the current directory
Eg:
huxd0275:/home/p1216319/venkat# df -k .
Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
/dev/hd1 1048576 1046688 1% 279 1% /home
10) Reporting Desk usage: Du : desk usage . this command will report the recursive examination of
the directory tree
huxd0275:/home/p1216319/venkat# du /home/p1216319
96 /home/p1216319/venkat
120 /home/p1216319
Access Control
1) File Permissons:
huxd0275:/home/p1216319/venkat# ls -lt
total 88
-rwxr-xr-x 1 p1216319 staff 253 Jul 23 09:59 emp.lst1.Z
-rw-r--r-- 1 p1216319 staff 102 Jul 23 09:49 uniqlist
E-mail
huxd0275:/home/p1216319/venkat# mail
Venkatreddy.Vennapusa@marksandspencer.com
Subject: hi
how r u
Cc:(ctrl- D)
Status Commands
1)stty
huxd0275:/home/p1216319/venkat# stty –a
The stty command sets certain terminal I/O options for the device that is the current standard
input
Finding the unused files: some of the files unaccessed or unmodified for months
Options
-i ignore case :
|marketing|31/12/76|8000
|marketing|12/31/74|9000
Process
1) ps : command : process statues
huxd0275:/home/p1216319# ps
PID TTY TIME CMD
311430 pts/0 0:00 ps
626818 pts/0 0:00 -ksh
PID: process id
TTY: terminal name
TIME: Cumulative process time
CMD: Process name
Kill –l
(-l) displays the available kill signals
huxd0275:/home/p1216319/venkat# kill -l
1) HUP 14) ALRM 27) MSG 40) bad trap 53) bad trap
2) INT 15) TERM 28) WINCH 41) bad trap 54) bad trap
3) QUIT 16) URG 29) PWR 42) bad trap 55) bad trap
4) ILL 17) STOP 30) USR1 43) bad trap 56) bad trap
5) TRAP 18) TSTP 31) USR2 44) bad trap 57) bad trap
6) ABRT 19) CONT 32) PROF 45) bad trap 58) RECONFIG
7) EMT 20) CHLD 33) DANGER 46) bad trap 59) CPUFAIL
8) FPE 21) TTIN 34) VTALRM 47) bad trap 60) GRANT
9) KILL 22) TTOU 35) MIGRATE 48) bad trap 61) RETRACT
huxd0275:/home/p1216319/venkat# kill -9 pid
• minute (0-59),
• hour (0-23),
30 18 * * * rm /home/someuser/tmp/*
Hour (0-23)
minute (0-59