find Command Training Material
Understanding UNIX Command: find
8/29/2011
Retail and Distribution – Business Intelligence
Nikhil D Patil
n.patil@tcs.com
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.
Syntax: find path [expression]
- Path: Unix Location from where search should start
- Expression: Any argument starts with ‘-’, ‘(’, ‘)’, or ‘!’ operators are considered as
Expressions. Everything before these operators are paths and everything
after that is part of expressions.
If we do not specify any Expression then –print is the default one.
Mentioning path with find command:
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.
Command: find / –name knowmaxDB.dat
Description: Command will search for file knowmaxDB, and search will start from Root
Directory.
Command: find . /ultimatix/knowmax/ $HOME –name knowmaxDB.dat
Description: Above command can be used if you have to search for a file at multiple locations.
Here we are searching “knowmaxDB.dat” file at Current Directory,
“/ultimatix/knowmax/” directory and Home directory.
Internal Use 2
find Command Training Material
Different Search Expressions:
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.
Below are the some Search expressions:
Search Expressions Description
-name Search file by its name
-type Search file by type e.g. file, directory, link, PIPE file, etc
-perm Search by file permissions
-atime Search by file last access time
-mtime Search by file last modification time
-ctime Search by file last Change time
Explanation with Examples:
-name: Used to search by filenames
Command: find /ultimatix/knowmax/ –name knowmaxDB.dat
If we have 3 files named Associate1_details.dat, Associate2_details.dat and
Associate3_details.dat.
To achieve this wild card characters can be used.
Command: find /ultimatix/Associates/ –name Associate?_details.dat
-type: Used to search by file types
Command: find /ultimatix/knowmax/ –type f
Command will list all files in “/ultimatix/knowmax/” folder.
Command: find /ultimatix/knowmax/ –type d
Command will list all directories in “/ultimatix/knowmax/” folder.
Command: find /ultimatix/knowmax/ –type p
Command will list all PIPE files in “/ultimatix/knowmax/” folder.
Command: find /ultimatix/knowmax/ –type l
Command will list all softlinks in “/ultimatix/knowmax/” folder.
Internal Use 3
find Command Training Material
-perm: Used to search by file permissions
Command: find /ultimatix/knowmax/ –perm 755
Command will list all files in “/ultimatix/knowmax/” folder with 755 file
permissions.
Command: find /ultimatix/knowmax/ –perm –o=w
Command will list all files in “/ultimatix/knowmax/” folder with write permissions
to others.
Command: find /ultimatix/knowmax/ –perm –u=x
Command will list all files in “/ultimatix/knowmax/” folder with execute
permissions to owner of the file.
-atime, mtime, ctime:
-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
Command: find /ultimatix/knowmax/ –atime -1
Command will list all files in “/ultimatix/knowmax/” folder which were accessed
between now and 1 day ago.
Command: find /ultimatix/knowmax/ –atime 1
Command will list all files in “/ultimatix/knowmax/” folder which were accessed
between 24 Hours and 48 Hours ago.
Command: find /ultimatix/knowmax/ –atime +1
Command will list all files in “/ultimatix/knowmax/” folder which were accessed
more than 48 Hours ago.
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
Advanced Use of find Command:
Use Case: Command to delete all files which are modified before some days.
Command: find /EXTRACTS/BACKUP/ -mtime +90 | xargs rm –f
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.
Command: find /EXTRACTS/BACKUP/ -size +1073741824c –exec ls -lh ‘{}’ \;
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.
Use Case: Command to find all files containing particular word.
Command: find /TEST/Unix_Scripts/ -name \*.ksh –print | xargs grep “transform.bteq”
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