Understanding Unix Command - Find Command
Understanding Unix Command - Find Command
Nikhil D Patil
[email protected]
Internal Use 1
find Command Training Material
Introduction:
‘find’ command is basic utility to search any file in a directory tree on Unix system.
find command does not return any output if file is not found.
It is a very powerful and useful command for analysis and maintenance work on Unix Server with
large number of files.
find command used along with any of the compression command i.e. tar, zip, gzip, etc. can be used
to perform periodic archival process. Also there are some advanced Use Cases mentioned in later
part of the document.
Along with find command path should e always mentioned. ‘.’ (Means Current Directory)
Or ‘\’ means root directory these operator can be used as path parameter.
Also any hardcoded path can be mentioned.
If we need to search at multiple locations at the same time then we can mention multiple
paths from where find command should start its search.
Some Examples:
Command: find .
Description: This is basic form of this command. Command will result all files present in Current
Directory and all sub directories within.
Internal Use 2
find Command Training Material
To search any file we need to provide search expression along with find command. These are
optional. If we do not mention then all files will be resulted.
But to restrict that result set we can provide some parameters like search by name, permissions of
the file, Owner of the file, File type, etc.
Internal Use 3
find Command Training Material
-atime: Used to search all file which are accessed within certain period
-mtime: Used to search all file which are modified within certain period
-ctime: Used to search all file which are changed within certain period
Similarly all files which were modified can be found using –mtime search criteria.
If file data is changed then file is considered as modified.
Ctime considers if file is changed like permissions, owner, group etc. is changed or file data is
modified.
So, if mtime of file is changed then ctime of file also changes but vice versa is not true.
Internal Use 4
find Command Training Material
Use Case: Command to delete all files which are modified before some days.
Description: Above command will find and delete all files which are modified more than 90 days
before. To perform delete operation on files found ‘xargs’ command is used.
Use Case: Command to list all files which have size greater than some value.
Description: Above command will find all files which have size more than 1GB and will list
those files along with properties.
Similarly if we need to find all files having size less than a mentioned size, then
command will be:
find /EXTRACTS/BACKUP/ -size -1024c –exec ls -l ‘{}’ \;
Above command will list all files having size less than 1KB.
Description: Above command will find all .ksh scripts first, then it will do the grep on those
scripts for word “transform.bteq”.
This command will return all lines from .ksh scripts having transform.bteq word in
it.
Above command is very useful for analysis purpose. It can be used to find any script
is getting called from how many parent scripts.
Internal Use 5