UNIX COMMANDS
1. $ LOGNAME : It displays the current user
information
2. $PWD : present working
directory
3. $DATE :
It displays the system date & time
4. $clear : To clear the screen
5. $cal :
it current month and year
6. $cal 2000 : Displays the 2000 year calendar
7. $cal 8 2006 : displays the 8th
month of 2006
8. $exit or logout : exit from current user account
9. $ who :
displays the all user in who are currently working on server
10. $finger : displays the all user who
are currently working on server with more information
11. Who am I :
displays current user information
12. Which or
where : displays the location of the given command
My Commands:
Du
–sk filename : to find the size of one file
Du –sk
test.txt
Du
–sk filename1 filename2 …. : to find size of multiple
files
Du –sk test.txt test2.txt
Du
–a ‘Directory name’ :
Give the size of all files in that directory.
Du –sk * also can be used to find size of all the
files.
Du
–a ‘In Bound’ : It List size of all
files/Folder size in present directory
Du -[akhrstx]: Du
for folder can be used with any of these characters
But I am getting same result for Du –[trx]
Du –h ‘In Bound’ : consider size of
directory while calculating
E:\files
in ds>du -h
512B ./In
Bound/testing_UNIX
14.50KB ./In
Bound
153.50KB ./Out
Bound
327.00KB .
How to find all the files in all the
directories in that folder?
Suppose Files in Ds is the folder which
has so many files and also 2 Folders In Bound and Out Bound. We want the files
present in In Bound and also Out bound, In that case what needs to be used?
Ls
–R
Syn: $which pwd
13. Cat : is use to create
new files or to open exiting files or to append data to the exiting files
Create:
cat >filename
-----------
-----------cntl+d
Redirect:
cat
file1 file2 file3 >file4--------àredirect output
Append:
cat >>filename--------à single file $cat file1 file2 file3 >>EMP------àmulti files
-----------
-----------cntl+d
Open
file: cat <filename-----àopen single
file $cat file1, file2, file3----------àto
open multi files
Cat >.filename---------for hidden files
14. Touch : It is used to create an empty file i.e.
0 byte file
SYN: $ touch filename
$ touch file1 file2
file3---------à create multiple
files
15. rm : deleting files or directories
EX
rm filename----------------àdeleting
single file
rm -i
filename--------------àdeleting files with
confirmation
rm file1
file2----------------àdeleting multiple
files
rmdir dirname--------------àdeleting
the directory but the directory must be empty
rm -r dirname-------------àdeleting
directory recursively (i.e. with tree str)x
rm –ri dirname-------------àremove
directory with confirmation
rm * ---------------------à
it delets all files
rm -I *--------------------àdelets
all files with confirmation
rm t* ---------------------àit
delets whose file name starts with ‘t’
How to delete the directory which is not empty?
Ex: Rm –r Testing_Unix.
16. mkdir : creating directories
Syn: $mkdir dirname
Sys: $Mkdir .dirname------------àhidden
directory
17. Cd : change directory
Sys: $cd abc
$pwd------/home/madhav/abc
using above cmd we can come out from abc now we at //home/madhav
cd..---------------------------àto
come out from current directory
cd../..------------------------àparent
directory
cd/---------------------------àit
changes to root directory
cd ~-------------------------àit
changes to home directory
18. cp :copy
source file to target file
Ex
Cp emp1 emp2----------------------àemp1
tp 2 coping
Cp –I m1 m2-------------------------àoverwrite
confirmation? Y
Cp –R source directory to target
directory-------cp –R abc xyz
19. mv :
it is used to rename or move file
Ex: mv exiting filename
new filename
Mv
emp .emp----------------------àto
hide
Mv .emp emp--------------------àto
unhide
20.ls :
display, list of all files & directories in a current directory
21.ls|more : display, list of all files &
directories page by page
22. ls –a : display, list of all files
& directories including hidden files and dirctories also in current
directory
23. ls –r :
display list of all files & directories revers order in a current directory
24. Ls –R : display list of all files & directories recursively in a
current directory
25. Ls –t : display list of all files
& directories according to date of creation in a current directory
26. Ls –F : display all list of files
& directories, link files, .exe files in a current directory
27. Ls-x : display all list of files
& directories according to width wise in a current directory
28. Ls-L : display all list of files
& directories in a current directory in a long list i.e. 9 fields
1) File
types
[1)
-- For regular file ii) d—for dir file III) L—for link file IV) b—for block of filesV) c— for char files] [here IV, V is device files]
2)
File permissions 3) no. of links 4) owner name 5) group name 6) file size in bytes
7) Date 8) time 9) filename
30. cmp : it compares two files char by
char
Sys: cmp file1 file2
E:\files in ds\In Bound>cmp type1.txt type2.txt
type1.txt type2.txt differ r: char 25, line 2
Ex: a1: Hello Good Morning
a2:
Hello Good Evening
If two lines are same then return nothing
If two files are are different then it
displays line number with character position
31. comm. : It display common lines b/w 2 files
Syn: Comm File1 file2
32. diff :
it display different lines b/w 2 files
33. pg :
it display the file contents page by page
Syn: $Pg filename
34. more : it also display the file
contents page by page
Syn: $more filename
34. head :it display the 1st n lines
from the file
Sys:
$ head –n filename
35. tail :
it display the last n lines from file
Syn: tail –n filename
Tail +n
filename----àit indicates nth
line to end of the line
Ex: tail +30 file
(in this file total no of records is 100) it displays the records from 30th
to 100
36. wc :
it counts the no of lines, words, chars in a given file.
Syn: $wc filename
i) wc –l filename------------------àit
gives the no of lines in a given file
ii) wc –w filename---------------àit
gives the no of words in a given file
III) wc –c filename---------------à
it gives no. of char in file
iv) wc -lw filename--------------à
it gives the no of lines and character in a given file
37. WILD CARD CHARCTERS or META
CHARCTERS
i) ‘*’--------àit
matches ‘0’ or more chars
ii) ‘?’ -------àit matches any
single chars
iii)
[ ] ---àit matches any
single chars in the given list
iv) ‘–‘------àit matches any single char in the given
range
1)
ls
t*---------------àit list the files
starts with ‘t’
2)
ls
*s -------------àit
list the files that ends with ‘S’
3)
ls b*k--------------àit
list the files starts with ‘b’ and ends with ‘k’
4)
ls
a?--------------àit list the 2 characters
filename 1st later fallowed by ‘a’ and second letter is any one character
5)
ls[bknt]-----------àit
list the files starts with ‘b’ or ‘k’ or ‘n’ or ‘t’
6)
ls [abcdefgh] or ls[a-h]*--àlist
the files, first char b/w ‘a’ to ‘h’
7)
ls [b-k][p-t][d-n]*---------à
it list the files the 1st character ‘b’ to ‘k’ 2nd char
‘p’ to ‘t’ 3rd char ‘d’ to ‘n’ after that any no of characters
ls
doesn’t care about Case sensitive
ex:
ls [aeiou]*.txt and ls[AEIOU]*.txt both will give the same result
38. grep : [globally search a regular expression and print it]
Is
used for to search a string or regular expression in a given file(s)
I)
Eg: $ grep madhav sample
O/p:
2nd line
5th
line
7th
line
ii) Eg: $grep madhav a1, a2, a3:
a1:------
a2:------
a3:------
iii) $ grep techno *------àit
searches for techno in current dir files (all files)
iv) $grep techno soft sample-----àit
searches for more than one word
We kept it in “ “
v) $grep
“techno soft” sample
grep
cmd options
$ grep
–i techno sample-----------------------àignore
case sensitive
$ grep –c techno sample-----------------------àcount
no of lines
$grep –n techno sample-----------------------àprint
along with the line numbers
$grep –l techno *------------------------------àlist
only file names
$grep –v “techno soft” sample---------------ànot
matches print the lines
$grep –ci techno sample----------------------àIgnore
case sensitive found no of lines
$grep “techno *” sample---------------------àpattern
Regular
expression: any string contains wildcard charctor knows
as regular expression or pattern
These
patterns are of 3 types:
Charctor
pattern: the default pattern is char pattern only
i) $grep “techno *” sample
ii) $grep “b [aeiou] ll”
sample
iii) $grep “b..d”
sample---ài.e. or matches any
single charctor
Word
pattern :/< />
/< -------àstart
of the word
/>------àend
of the word
Grep
“/<techno/>” sample ------------ào/p:
techno
Grep
“/<techno” sample----------------ào/p:
techno soft
-----------------ào/p:
techno 123
Grep
“techno/>” sample ----ào/p:
hellotechno, abctechno
Grep
“/< [0-9][0-9][0-9][0-9]/>” sample--------------àit
display 4 digits (i.e.1234, 4567)
Line pattern: ^----------starts
of the file
$----------ends
of the file
Ex: grep “^d” sample---------------àit
display the line starts with‘d’
Grep “^the” sample-----------------àit
list the lines start with ‘the’
Grep “^/<the/>” simple ----------àsample
the line exactly start with ‘the’
Grep “t$” simple-------------------àlist
the line ends with‘t’ or last char is‘t2019
Grep “[0-9] $” sample------------à display
the line ends with 0 to 9 digit
Grep “^ [bkt]” sample------------àlist
the line starting with ‘b’ or ‘k ‘or’t’
Grep “^ [^bkt]” sample----------àlist
the line which is not start with ‘b’ or ‘k’ or‘t’
Grep “”^UNIX$” sample ---------àdisplay
the line having only word ‘Unix’
Grep “^…. $”----------------------àlist
the line which contains ‘4’ characters
(.)----------------------------------àrepresent
single charctor
Grep “^.”------------------------àSample
it list all lines
Grep “/.” Sample: --------------àit
lists the line start with (.)
We use \ to search *, $, ^
as a charctor-------à i.e.\*,|^,\$
Grep “^$” sample--------------à
it list empty lines
Grep –c “^$” sample----------àcounts
no of empty lines in a file
Grep –v “^$” sample----------àprint
not matches (i.e. not empty) lines
Grep –v “^$”sample
>temp
Mv temp
sample-------------------àhere both are
delete empty files
This command is used to show the After 3 lines when the
string is found
Grep –A 3 –i “Chandra” emp.txt
This command is used to show the Before 3 lines when the
string is found
Grep –B 3 –I “se” emp.txt
If the no.of lines found are not exactly same as the
lines found then it will result the lines that it found.
39. fgrep : it is used for search multiple strings
but it doesn’t allow to search regular expression
$grep “hello
>techno
>UNIX”sample----------àit
searches for hello or techno str UNIX
40. egrep : it is combination of grep and
fgrep
$egrep “hello
>hello
>UNIX”sample $egrep “^$” sample
41. Sed : to replace a string
$ sed “s/existing string/new string/g” filename--àsed
is used to find and replace and grep is for find print
Here g is to
state that replace existing string with new string Globally. If we want to
replace only second instance then
Sed “s/ existing
string/new string/2”
We can use any
delimiter to replace the command
Sed “s| existing
string| new string |g”
We can replace
the string in only one particular line
Sed “3 s|
existing string| new string |g”
We can replace range
of lines by using below command
Sed “1,3 s| existing
string| new string |g”
We can find any no.of
lines 2 times by using sed command
Sed ‘p’ emp.txt -à
This will print all lines 2 times
We can print
only particular lines of a file
Sed –n ‘1p;$p’
emp.txt
We can delete lines by usin sed
Sed ‘2 d’ emp.txt àDelete 2 line
Sed ‘1d; $d ‘ emp.txt àDelete 1 and last line
Sed ‘1,5 d’ emp.txt àDelete the line range from 1 to 5.
i) $sed “s/Unix/Linux/gi “madhav: -------------à
‘i ‘for case sensitive
ii) $sed “s/^Unix/Linux/gi”----------------------à
whatever lines starting with UNIX are replaced by Linux
iii) $sed “s/^$/I like Unix/g I” sample--------àempty
string are filled with ‘I like UNIX’’
iv) $Sed “s/Unix//g I” sample-----------------àit
search UNIX if found replace with ‘nothing’ (empty)
42. tr : to translate a charctor
i) tr “a” “p”
<s--------------------à it read data from
sample and ‘a’ is replaced by ‘p’
ii) tr “aeiou”
“AEIOU”<sample----àreplace char by char
iii) tr “,” “lt”
<emp----------------àwhenever “,” is
there replace with tab space
iv) tr
“[a-z]” “[A-Z] <Siva--------à converts hole file into uppercase
43) Cut :
it is used for to retrieve required fields and characters from a given file
Ex:
madhav is good boy--à18 chars
Cut
–f 2-5 madhav---ào/p: adha
Cut
–c 1-10 madhav
Cut
–c 5-10, 15-20 madhav-------àfor
every line 5-10, 15-20 characters
Cut –c 1,2,3 emp.txt àPrints 1,2 and 3rd char of each line
Cut –c 1-3,4-6 emp.txt à prints 1 to 3 and 4 to 6 character of each line
Cut –c10- emp.txt àPrint the lines from 10th to end position by using cut command
We
can print the lines by using delimiter
Cut
–d’,’ –f2 emp.txt àThis will print the
2nd word of each line by taking ‘,’ as delimiter.
Cut
–d’,’ –f2,3, emp.txt àThis will print the
2nd and 3rd words of each line by taking comma as
delimiter.
How to get the last field of a file.
Cat emp.txt| rev| cut –d’,’ –f2 |rev
44) Paste : is used for to join two or more files
horizontally by using delimiter
Cat >states cat
>cities paste –d “:”states
cities>tr filename
AP Hyderabad AP:
Hyderabad
Tamilnadu madras Tamilnadu: madras
Karnataka Bangalore Karnataka:
Bangalore
Kerala Trivandrum
Maharashtra Bombay
45) Sort : it is used for to sort the file content.
By default it sorts file contents based on ASCII values-àdefault
is ascending
Sort sample
i) sort –r sample------àdisplays
descending order
ii) sort –u sample-----àit
displays unique lines in the given file
iii) sort –n file----à’N”
numeric comparisons
iv) sort –nur file
v) sort sample >temp
$mv temp sample
Sorting
the data field by field
+pos--àstarting
field
-Pos--àending
field (optional)
i) Sort –f +pas1 –pas2
filename
ii) Sort –f +1 -3
filename-----àstarting from end
before 3
iii) Sort –fn +2 -3 file
------àit gives only numbers
46) Uniq : it displays unique lines in the given
file but the file contents should be in sort order
Ex:file1 i)
$ uniq file ii) $uniq –u
filename--àit eliminates
duplicates Aaaaaa
Aaaaaa Aaaaaa ccccc
Aaaaaa cccccccc ddddd
Ccccccc dddddd Ppppp
Ddddd hhhhhh ttttttt
Hhhhh pppppp
Hhhhh ttttttttt
Ppppp
Ttttttt
iii) $ Uniq –d filename----displays only duplicated
lines IV) uniq –c filename-----àit
counts how many times lines duplicated
Aaaaaa-----2
Cccccccc----1
V) $ uniq –u file >temp
$ mv temp filename
Delete duplicated lines from file Ddddd-----1
Hhhhhh---2
Ppppp-----1
Ttttttt-----1
47)piping(|) : it is used
for to combine 2 or more cmds |take left side o/p to right side cmd as i/p
i) $who |wc –l---------àcount
total no of lines (files) in current directory
ii) $ ls|wc –l------------àdisplays
total no of subdirectories in the current directory
iii) ls –l |grep “^d”----àdisplays
total no of subdirectories who stats with line no‘d’
iV) $head -30 sample|tail
+20 sample------àdisplay the the
lines from 20 to 30 from given file
v) $ grep UNIX stud | cut
–f 2, 3|sort filename -----àdisplay
UNIX students names & ph no in ascending order
48) $tee : it is used
to for to write data to the file as well as to the screen
$grep
UNIX stud | cut –f 2, 3 |sort |tee file1
49)
Shell scripting:
It is group of UNIX
commands and shell keywords
The main concept of shell
scripting is to handle text files
I) Boune shell----àsteave
Bourne -----à$--------sh--- (sh
as interpreter)
ii) Bash shell (borne
again shell) ---same as above--àadvanced
version of Bourne is BASH (Linux default shell)
iii) Korn shell------David
korn----$----------ksh (interpreter) ----àused
as AIX default shell
Mostly used shell is korn
shell it supports re usability, all shell designed on bourn shell
50) $ksh : shift to
korn shell
51) $echo $0 : it displays
current child shell name
52)
$alias : it lists all alias names
53)
Unalias alias names: to delete alias names
54) $ history : it displays
the previously executed commands
55)
Echo :
it display the string on screen (monitor)
File
permissions:(xxx/xxx/xxx)
User/owner---permissions (first part)
Group permissions (second part)
Other permissions (third part)
+---
(add permissions to u/g/o but it does not delete exiting permission)
---
(deny permissions)
=----
(assign permissions (add permissions to u/g/o but it delete exiting permission))
rw-/rw-/r-- ---------àDefault
permissions for regular files
rwx/rwx/r-x---------àdefault
permission for directories
56) chmod : it is used for to change file
permissions
Syn: $chmod who/ [+/-/=]/
i) ls –l filename-------àrw-
rw- r—
ii) chmod g+x
filename-----àrw- rwx r—
iii) chmod u+x, g-w
filename------àrwx r-x r—
iV) chmod g=w
filename-----àrwx –w- r—
Octal
code
Read------4
Write-----2
Execute—1
$chmod 756 filename-------àrwx
r-x rw-
$chmod 642 filename----àrw-
r-- -w-
57) chown
: to change owner name of the file
#chown owner name filename
58) chgrep : to
change group name of the file
# chgrp group name
filename
58) $write : it is user for to with the users but the
user shxould be logged into the server
$write
techno2 ii) $mesg n--àdeny
the msg
Hello iii) mesg y-àto
allow msges
Cmtl+d
59) awk/nawk
file : scan for patterns in a
file and process the results
60) cat : concatenate (list)
or file
61) chsh
(paawd –e/-s) userlogin_shell: change the user login shell
62) df : report the
summary of disk blocks and nodes free and in use
i) df
–k---àit displays the disk space in bytes
ii) df –h---àit
displays disk space in kilo bytes
iii)
df –g---àit displays the disk space in giga bytes
63)du :it displays the directory wise disk usage in
form of blocks each block size is 512 bytes
64)g zip :to
create a zip file
Ex $gzip filename----ào/p:
filename.gz
To Create multiple Zip
files to a single Zip file we use below command
>Zip myzip.zip emp*.txt
The above command will zip all the files to Myzip.zip and
we can get all files starting with emp.
65) gunzip :
to unzip the files
Ex: gunzip filename.gz
66. compress : it also used for to zip the file---àit
used to save with .z format
67) Uncompress : same as above
Gzip
saves more memory than compress
68) zcat :
it used to displays zip file contents in readable format
$zcat sample.gz
Or
$zcat sample.z
69) To kill foreground
job cntl+c or cntl+z
$ sleep 500
Cntl+c
$ sleep 100&
70) ps or $ ps
–f : it displays current user
account running process list (show status of active process)
71) $ps –a:
it displays all user accounts running process list
72) Kill : it kill background process
Ex: kill PID
73) telnet :
to connect to remote server
74) ftp:
file transfer protocol -----àtransfer
files from one server to another
$ ftp
ipaddress
Login: -------
Password: ------
Ftp>ls
(server)
Ftp>|ls(client)
Ftp>get filename (to download a file)
Ftp>mget file1 file2----- (to download multi files)
Ftp>put filename (to upload a file)
Ftp>mput file1 file2----- (to upload multi files)
75)Ftp: to transfer
files from one server user account to another server user account
$su root --àto
switch to admin
76) Wall : it is used for to sent broadcast message to
all users who are currently working on server
$wall
Happy new-year
Cntl+d
77)mail :it is used for to send the mail, if
user is not logged in then also we can send mail
i) $ mail techno1
Cntl+d every user contains
mailbox
at a time we can send msg
to multiple users
2) $mail techno1 techno2
techno 3
Sub: from techno
Cntl+d
iii) $ mail techno<stud
stud send content as a
mail to techno3
mail is the cmd to read
mails in the mailbox
$mail
1>first mail
2>second mail reading
&2 it quickly opens
second mail
&q--àquit
from mail box
&w to save mail
contents to a file
&pàprint
mail contents
&r--àreplays
&d-àdelete
mails
78)$mail –f:to
read mails send to secondary mailbox
79) emacs :full screen editor
80) echo : echo the text string to on monitor
81) file : classify the file type
82) expr : evaluates the arguments, used to do
arithmetic,etc in the shell
83) find :
find files, matching a type or pattern
84) Hostname
: display or set the name of the
current machine
85)ln : link the source to target
86)lpq ,lpstat :show the status of the print
jobs
87)lpr,lp :print
to defind printer
88) lprm, cancel : remove a print job from the print quee
89) man :display manual of given cmd
90)od :octal dump a binary file,in
octal,ASCII,hex,decimal or char
91)passwd :to set or change password
92) pr : filter the file and print
it on the terminal
93) rcp hostname : remotely copy files from this machine to
another machine
94) rlogine hostname:
login remotely to another machine
95) rsh hostname : remote shell to run on another machine
96) script file : saves everything that appears
on the screen to file until exit is executed
97) source file : read cmds from the file and
execute them in the current shell
98) string file : used to search binary files for
ASCII strings
99) Sty : set or display terminal control
options
100) uudecode
file :
decode a uuencoded file, recreating the original file
101) uuencode new
name : encode the binary
file to 7-bit ASCII,usefull when sending via email, to be Decode as new name at
destination
102) vi :
visual ,full screen editor
103) jobs : display background and
suspended processes
104) kill %1 : remove suspended process #1
105) top : display the current, most
computer-intensive commands
106) osview : display the operating system
statistics
107) setenv : list environmental variables