You are on page 1of 33

UNIX Commands

-R. Seetha Rama Guptha


[09MX38]
Commands
• comm
• cmp
• diff
• spell
• find
The comm Command
Select or reject lines common to two files.

comm [-1][-2][-3] file1 file2

Option Description
-1 Suppress the output column of lines unique to file1.
-2 Suppress the output column of lines unique to file2.
-3 Suppress the output column of lines duplicated in
file1 and file2.
Example
$ comm file1.txt file2.txt
apple FILE1.TXT FILE2.TXT
ball
cat apple ball
doll ball cat
elephant cat fox
fox doll gun
gun elephant house
house
Example
$ comm -1 -2 file1.txt file2.txt
ball FILE1.TXT FILE2.TXT
cat
apple ball
ball cat
cat fox
doll gun
elephant house
The cmp Command
Compares two files and tells you which line
numbers are different.
cmp[-c][-i N][-l][-n N] file1 file2

Option Description
-c Output differing bytes as characters.
-i N Ignore differences in the first N bytes of input.
-l Write the byte number (decimal) and the differing
bytes (octal) for each difference.
-n N Compare at most N bytes.
Example
$ cmp file1.txt file2.txt
file1.txt file2.txt differ: byte 8, line 2

FILE1.TXT FILE2.TXT
apple apple
ball bell
cat cap
Example
$ cmp -c file1.txt file2.txt
file1.txt file2.txt differ: byte 8, line 2 is 141 a 145 e

FILE1.TXT FILE2.TXT
apple apple
ball bell
cat cap
Example
$ cmp -i 6 file1.txt file2.txt
file1.txt file2.txt differ: byte 2, line 1

FILE1.TXT FILE2.TXT
apple apple
ball bell
cat cap
Example
$ cmp -n 10 file1.txt file2.txt
file1.txt file2.txt differ: byte 8, line 2

FILE1.TXT FILE2.TXT
apple apple
ball bell
cat cap
Example
$ cmp -l file1.txt file2.txt
8 141 145
14 164 160

FILE1.TXT FILE2.TXT
apple apple
ball bell
cat cap
The diff Command
Displays two files and prints the lines that are
different.

diff[-i][-c][-w][-e][-h][-l][-r]
[-s][-S name] file1 file2
The diff Command
Option Description
-i Ignore Case.
-c Produces a listing of differences with three lines of context.
-w Ignores spaces and tabs.
-e Produces a script of only a, c, and d commands .
-h It works only when changed stretches are short and well
separated.
-l Produce output in long format.
-r Applies diff recursively to common subdirectories encountered.
-s Reports files that are the identical; these would not otherwise
be mentioned.
-S name Starts a directory diff in the middle, beginning with the file
name.
Example
$ diff -i file1.txt file2.txt
1d0
< alligator
3c2
< cat File1.Txt File2.Txt
---
>ct alligator Bull
5c4 bull ct
< elephant
--- cat doll
> elephant doll elephant
7a7 elephant fox
> hyena
fox giraffe
giraffe hyena
Example
$ diff -i -w file1.txt file2.txt
1d0
< alligator
3c2
< cat File1.Txt File2.Txt
---
>ct alligator Bull
7a7 bull ct
> hyena
cat doll
doll elephant
elephant fox
fox giraffe
giraffe hyena
Example
$ diff -c -i file1.txt file2.txt
*** 1,7 ****
- alligator
bull
! cat
doll
! elephant File1.Txt File2.Txt
fox
giraffe alligator Bull
--- 1,7 ---- bull ct
Bull cat doll
!ct
doll doll elephant
! elephant elephant fox
fox
giraffe fox giraffe
+ hyena giraffe hyena
Example
$ diff -e -i file1.txt file2.txt
7a
hyena
.
5c
elephant File1.Txt File2.Txt
. alligator Bull
3c
ct bull ct
. cat doll
1d doll elephant
elephant fox
fox giraffe
giraffe hyena
Example
$ diff dir1 dir2
Only in dir1: file2.txt
Only in dir1: file3.txt

DIR1 DIR2
file1.txt file1.txt
file2.txt
file3.txt
Example
$ diff -s dir1 dir2
Files dir1/file1.txt and dir2/file1.txt are identical
Only in dir1: file2.txt
Only in dir1: file3.txt
DIR1 DIR2
file1.txt file1.txt
file2.txt
file3.txt
Example
$ diff -S file3.txt dir1 dir2
Only in dir1: file3.txt

DIR1 DIR2
file1.txt file1.txt
file2.txt
file3.txt
The spell Command
Select or reject lines common to two files.
Spell filename

File1.Txt
The common use for Notepad is to edit text (.txt) files

$ Spell file1.txt
txt
The find Command
Search for a file based on a criteria

find [-atime n][-ctime n][-mmin n]


[-cmin n][-name fname][-maxdepth n]
[-perm]
The find Command
Option Description
-atime n True if the file was accessed n days ago. The access
time of directories in path is changed by find itself.
-ctime n True if the file's status was changed n days ago.
-name This will search the whole system for any file with a
given name.
-maxdepth -N Descend at most N levels of directories below the
command line arguments.
-mmin –N Locate files modified less than N minutes ago.
-cmin –N Locate files changed less than N minutes ago.
-perm Find the files based on permissions to the users.
Example
$ find
.
./file2.txt
./dir2
./dir2/file1.txt
./input.txt
./file1.txt
./dir1
./dir1/file2.txt
./dir1/file3.txt
./dir1/file1.txt
./note.txt
Example GUPTHA
.
$find -name file1.txt ./file2.txt
./dir2
./dir2/file1.txt ./dir2/file1.txt
./file1.txt ./input.txt
./dir1/file1.txt ./file1.txt
./dir1
./dir1/file2.txt
./dir1/file3.txt
./dir1/file1.txt
./note.txt
Example GUPTHA
.
$find -maxdepth 1 -name file1.txt ./file2.txt
./dir2
./file1.txt ./dir2/file1.txt
./input.txt
./file1.txt
./dir1
./dir1/file2.txt
./dir1/file3.txt
./dir1/file1.txt
./note.txt
Example GUPTHA
.
$find dir1/ -name file1.txt ./file2.txt
./dir2
dir1/file1.txt ./dir2/file1.txt
./input.txt
./file1.txt
./dir1
./dir1/file2.txt
./dir1/file3.txt
./dir1/file1.txt
./note.txt
Example GUPTHA
.
$ find dir1/ -name \*.txt ./file2.txt
./dir2
dir1/file2.txt ./dir2/file1.txt
dir1/file3.txt ./input.txt
dir1/file1.txt ./file1.txt
./dir1
./dir1/file2.txt
./dir1/file3.txt
./dir1/file1.txt
./note.txt
Example GUPTHA
.
$ find -mmin -10 ./file2.txt
./dir2
. ./dir2/file1.txt
./file1 ./input.txt
./file1.txt
./dir1
./dir1/file2.txt
./dir1/file3.txt
./dir1/file1.txt
./note.txt
Example GUPTHA
.
$ find -cmin -10 ./file2.txt
./dir2
. ./dir2/file1.txt
./in.txt ./in.txt
./file1.txt
./dir1
./dir1/file2.txt
./dir1/file3.txt
./dir1/file1.txt
./note.txt
Example GUPTHA
.
$ find -perm -o=r ./file2.txt
./dir2
. ./dir2/file1.txt
./dir2 ./in.txt
./dir2/file1.txt ./file1.txt
./file1.txt
./in.txt ./dir1
./dir1 ./dir1/file2.txt
./dir1/file2.txt ./dir1/file3.txt
./dir1/file3.txt ./dir1/file1.txt
./dir1/file1.txt ./note.txt
./note.txt
Example(s)
$ find -mtime 0
 Find files modified between now and 1 day
ago (i.e., within the past 24 hours)

$ find -mtime -1  less than 1 day ago


$ find -mtime 1  between 24 & 48 hours ago
$ find -mtime +1  more than 48 hours ago

$ find -mmin +5 -mmin -10


 between 6 and 9 minutes ago
Thank U

You might also like