Useful Find command in unix with examples

Find command in Unix decoded

Find command in unix with examples

Find command in Unix 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.
conditions of find

The basic syntax of the find command is:
find path options
where path is the path to search
and options are the options given to find command.
-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.
-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 nnn find files whole permission flags match octal number nnn.
-type l/d/f find links,directory and files
-size <size> Find files greater than given size

Find command in unix with examples

In all our below Find command in unix with examples, the path is our current directory and hence we use .(dot).

1. To find files modified in the last 2 days:
find . -mtime -2

2. To find files modified before 1 days:
find . -mtime +1
3. To find files modified in the last 10mins:
find . -mmin -10
4. To find files modified before 10mins:
find . -mmin +10
The above commands will find both files and directories modifying the criteria. If you want to find only files, use the -type option.
find . -type f -mmin -40
This will find only the files modified in the last 40 mins, not directories.

5.Show all links

find . -type l -exec ls -ld {} \; all directiries
find . -type d -exec ls -ld {} \;

More Find command in unix with examples

find $HOME -print will lists all files in your home directory.
find /work -name x1 -print will list all files named x1 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.
find . -type f -size +100000 -exec ls -al {} \;
find . -atime +1 -type f -exec mv {} TMP \; # mv files older then 1 day to dir TMP
find . -name “-A” -exec rm {} \; # a script error created a file called -A
find . \! -name “*.Z” -exec compress -f {} \; Compress all the file which are not compressed
find . -type f \! -name “*.Z” \! -name “.comment” -print | tee -a /tmp/list
find . -name *.ini  : Seach all the files like *.ini
find . -exec chmod 755 {} \;  Change the permission of all the files in the directory to 755
find . -atime +30 -exec ls –l {} \; | wc -l
find . -name xeroxrelease* -print 2>/dev/null
find . -name ‘tmp*’ -depth -exec rm {} \;
find . -name ‘tmp[0-9][0-9]*’ -depth -print | tee -a /tmp/tmpfiles

Some more information about find command in Unix
-mmin option of find is not available in all Unix flavours . If you dont have the mmin option, use the following:
Step 1:First create a dummy file whose timestamp is the time you are looking for:
touch -d “10 mins ago” temp
The above touch command will create a temp file whose timstamp is 10mins before. For example, if the time now is 8hours 50mins, the temp file timstamp will be 8hours 40mins.
If your Unix flavor does not have the “-d” option in the touch command, you can use the following method to set the timestamp:
touch -t 1006021020 temp This creates a temp file whose time stamp is 2010,June 2, 10hours 20mins. [YYMMDDHHMM]
Step 2: search files which are modified after this file temp has been modified. The below command will display all the files modified after the temp has been modified OR in other words find files which are newer than temp file:
find . -newer temp
Similarly, to find files which are modified before 10 mins. In other words to negate the above search, use the exclamation:
find . ! -newer temp
In the same way, we can find files modified from any time we need.

Links of other resources on Find

Find command from Wikipedia

Find command more examples


Hope you like this post on Useful Find command in unix with examples. Please try at your end and let me know if it helps you in anyway. Please do provide the feedback.  If you like the  post, please like this page on facebook