Saturday, 28 January 2012


find  command helps you to find files in a directory  structure much faster than ls command for
various options. It can widely be used for getting the list of files in a directory  and its sub directories
with a given condition on file time stamp, size, name and so on.

Example 1) Suppose that you want to know the entire directory  structure in a
Particular path, then use the find command as follows.
 /home/Varsha $  find   .  –type  d
./ebooks/perl/11.Ref and REcs
./ebooks/perl/12.Packages, Libraries, and Modules
./ebooks/perl/13.Classes, Objects, and Ties
./ebooks/perl/14.Database Access
./songs/hindi/songs collection/15 jab we met
./songs/hindi/songs collection/1942 A Love Story
./songs/hindi/songs collection/20 hey baby
./songs/hindi/songs collection/22 jhoom barabar jhoom
././songs/hindi/songs collection/26 let the music play 3++
./songs/hindi/songs collection/Aa Dekhe Zara
./songs/hindi/songs collection/Aap Kaa Suroor
./songs/hindi/songs collection/Aashiq Banaya Aap Ne
./songs/hindi/songs collection/Rockstar
./songs/hindi/songs collection/3 idiots
It seems varsha listens to these  songs while her boss is not around .

This command searched all the directories  starting from  /home/Varsha  and displayed it.
Similarly for displaying all the files you can use   -type f option. Here the   ‘.’  In the command refers to the current directory.
You can use any directory name as the base directory for find to search

To  display everything(i:e  all
Directories and  files) use.
find  .  - print

Example 2)
You want the output to be similar to that of ls command, use find as follows.
 /home/Varsha $  find  - type  f  -ls
244269   33 -rw-r--r--   1 Varsha  Trainee       60 Nov  9 23:58 ./ebooks/perl/1.Strings/chapter1
235915   86 -rw-r--r--   1 Varsha  Trainee      215 Nov  9 11:26 ./ebooks/perl/1.Strings/chapter2
104841   25 -rw-r--r--   1 Varsha  Trainee      554 Nov  9 11:26 ./ebooks/perl/1.Strings/chapter3
104838   32 -rw-r--r--   1 Varsha  Trainee      510 Nov  9 11:26 ./ebooks/perl/10.Subroutines/chapter1
104840   33 -rw-r--r--   1 Varsha  Trainee       55 Nov  9 11:26 ./ebooks/perl/10.Subroutines/chapter2

The output you get are the values included in the following order
I-node number                    
 Size in kilobytes (1024 bytes)  
 Protection mode                 
 Number of hard links            
 Size in bytes                   
 Modification time               

Example 3)
 Find also helps in retrieving the files which are a modified  n days before or modified after n days. Where  n is any integer.
Suppose you want the list of all the files which are modified   5 days  ago so that it can be removed after archiving It.
/home/varsha $ date
Fri Dec 23 21:16:06 IST 2011
/home/varsha $  find   . –type  f  -mtime +5     >file_list_5

Then use this file list(file_list_5)  to take a tar of the file, or to remove those files and save this file as a  copy of ‘which files have been archived’.
Similarly you can use -5  instead of  +5 for files modified after 5 days.

Example 4)
to find all the find all the files which are greater or less than a particular size, use –size option.

Suppose you want the list of files which are of size greater than 100kB,use find as follows.
/home/varsha $ find  .  –size  +100c

Example 5)
To find all files  which are created after the modification time of a particular file,use find with -newer option.
If you want to list all the files that were created after 2013-05-31 06:25 

/home/varsha/$ touch -t 201305310625  test_file

home/varsha/$  ls  -lrt   test_file
244269   33 -rw-r--r--   1 Varsha  Trainee       0 May 31  06:25

home/varsha/$ find . -newer  test_file


You can verify it by using -ls option.


No comments:

Post a Comment