Unix Commands Part 3

Comparison and Searching

diff,dircmp, cmp, grep, find.


Diff command.
diff command will compare the two files and print out the differences between.
Here I have two ascii text files. fileone and file two.
Contents of fileone are

This is first file  this is second line this is third line this is different    as;lkdjf  this is not different

filetwo contains
This is first file this is second line this is third line this is different    xxxxxxxas;lkdjf this is not different

diff fileone filetwo will give following output
 4c4 <> this is different    xxxxxxxas;lkdjf

Cmp command.
cmp command compares the two files. For exmaple I have two different files fileone and filetwo.
cmp fileone filetwo will give me

fileone filetwo differ: char 80, line 4 

if I run cmp command on similar files nothing is returned.
-s command can be used to return exit codes. i.e. return 0 if files are identical, 1 if files are different, 2 if files are inaccessible.
This following command prints a message 'no changes' if files are same
cmp -s fileone file1 && echo 'no changes' .
no changes

Dircmp Command.
dircmp command compares two directories. If i have two directories in my home directory named
dirone and dirtwo and each has 5-10 files in it. Then
dircmp dirone dirtwo will return this

 Dec  9 16:06 1997  dirone only and dirtwo only Page 1     ./cal.txt                                   ./fourth.txt ./dohazaar.txt                              ./rmt.txt ./four.txt                                  ./te.txt ./junk.txt                                  ./third.txt ./test.txt 

Grep Command
grep command is the most useful search command. You can use it to find processes running on system, to find a pattern in a file, etc. It can be used to search one or more files to match an expression.
It can also be used in conjunction with other commands as in this following example, output of ps command is passed to grep command, here it means search all processes in system and find the pattern sleep.
ps -ef | grep sleep will display all the sleep processes running in the system as follows.

     ops 12964 25853  0 16:12:24 ttyAE/AAES  0:00 sleep 60      dxi 12974 15640  0 16:12:25 ttyAH/AAHP  0:00 sleep 60      ops 12941 25688  0 16:12:21 ttyAE/AAEt  0:00 sleep 60      ops 12847 25812  0 16:11:59 ttyAH/AAH6  0:00 sleep 60      ops 12894 25834  0 16:12:12 ttyAE/AAEX  0:00 sleep 60      dxi 13067 27253  2 16:12:48 ttyAE/ABEY  0:00 sleep 1      ops 13046 25761  0 16:12:44 ttyAE/AAE0  0:00 sleep 60      dxi 12956 13078  0 16:12:23 ttyAG/AAG+  0:00 sleep 60      ops 12965 25737  0 16:12:24 ttyAE/AAEp  0:00 sleep 60      ops 12989 25778  0 16:12:28 ttyAH/AAHv  0:00 sleep 60      ssb 13069 26758  2 16:12:49 ttyAH/AAHs  0:00 grep sleep      pjk 27049  3353  0 15:20:23 ?           0:00 sleep 3600
    Options:
  • -b option will precede each line with its block number.
  • -c option will only print the count of matched lines.
  • -i ignores uppercase and lowercase distinctions.
  • -l lists filenames but not matched lines.

other associated commands with grep are egrep and fgrep. egrep typically runs faster. for more information type man egrep or man fgrep in your system.


Find command.
Find command is a extremely useful command. you can search for any file anywhere using this command provided that file and directory you are searching has read write attributes set to you ,your, group or all. Find descends directory tree beginning at each pathname and finds the files that meet the specified conditions. Here are some examples.

Some Examples:
find $HOME -print will lists all files in your home directory.
find /work -name chapter1 -print will list all files named chapter1 in /work directory.
find / -type d -name 'man*' -print will list all manpage directories.
find / -size 0 -ok rm {} \; will remove all empty files on system.

conditions of find

  • -atime +n |-n| n will find files that were last accessed more than n or less than -n days or n days.
  • -ctime +n or -n will find that were changed +n -n or n days ago.
  • -depth descend the directory structure, working on actual files first and then directories. You can use it with cpio command.
  • -exec commad {} \; run the Unix command on each file matched by find. Very useful condition.
  • -print print or list to standard output (screen).
  • -name pattern find the pattern.
  • -perm nnnfind files whole permission flags match octal number nnn.
  • -size n find files that contain n blocks.
  • -type c Find file whole type is c. C could be b or block, c Character special file, d directory, p fifo or named pipe, l symbolic link, or f plain file.

Comments

Popular posts from this blog

Linux File and Directory Permissions

How to Disable SSL for Webmin

INSTALL CISCO VPN CLIENT ON WINDOWS 10 (32 & 64 BIT). FIX REASON 442