Home » Unix command and scripts » Find command

Find command

This Find command article is part of  Linux tutorials and it has detailed explanations, examples

Find command in Unix is an 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 the path is the path to search
and options are the options given to find command.

Options:
-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 descends the directory structure, working on actual files first and then directories.
-exec command {} \; 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 example

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

See also  sed delete line containing string

To find files with names like “test*”

find . -name "test*" -print
find file command in unix

To find files modified in the last 2 days:

find . -mtime -2

To find files modified before 1 day:

find . -mtime +1
find mtime command example unix


To find files modified in the last 10 mins:

find . -mmin -10
find mmin command example in Unix


To find files modified before 10 mins:

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.

Show all links

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

Find all directories

find . -type d -exec ls -ld {} \;
Unix find directory command

More Linux find command examples

Print will list all files in your home directory

find $HOME

Print will list all files named x1 in /work directory.

find /work -name x1

Print will list all man page directories.

find / -type d -name 'man*' -

Search all the files like *.txt
find . -name "*.txt"

Find command to prevent printing error message like permission denied while using find command

find . -name xeroxrelease* -print 2>/dev/null

Find and print files in the current directory and two-level down a directory and then copy that list /tmp/tmpfiles file

find . -name 'tmp[0-9][0-9]*' -maxdepth 2 -print | tee -a /tmp/tmpfiles

Using -exec option with find

Command to remove all empty files on the system.

find / -size 0 -exec rm {} \;

List all the files having more than 100000 bytes

find . -type f -size +100000 -exec ls -al {} \;

move (mv) files older than 1 day to dir TMP

find . -atime +1 -type f -exec mv {} TMP \;

Remove the error-file

find . -name "error-file" -exec rm {} \;

Compress all the files which are not compressed

find . \! -name "*.Z" -exec compress -f {} \;
find with exec command line option example

Change the permission(chmod) of all the files in the directory to 755

find . -exec chmod 755 {} \;

Count files that are accessed after 30 days

find . -atime +30 -exec ls –l {} \; | wc -l

Remove tmp* files in the current directory and one level down the directory

find . -name 'tmp*' -maxdepth 2 -exec rm {} \;

Find files that have written permission to group

find . -perm -g=w -type f -exec ls -l {} \;

find and replace in a range of files

find ./ -type f -exec sed -i 's/tom/harry/g' {} \;

Using xargs option with find

-exec command has some limitations like it can only run the specified command on one file at a time. we can use xargs with pipe to solve this problem. It enables users to run a single command on many files at one time

See also  What is shell and Shell Scripts

find . -name ‘tmp*’ -maxdepth 2 |xargs rm

find . -name “*.txt” |xargs grep -w “tom”

Some more information about find command in Unix with example


-mmin option of find is not available in all Unix flavors. 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 timestamp is 10 mins before. For example, if the time now is 8 hours 50 mins, the temp file timestamp will be 8 hours 40 mins.
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 timestamp is 2010, June 2, 10 hours 20 mins. [YYMMDDHHMM]
Step 2: search files that 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 that are newer than the temp file:

find . -newer temp

Similarly, to find files that 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 at any time we need.

Links of other resources on Find

Find command from Wikipedia

Find command more examples

Conclusion:

Hope you like this post on the Useful Find command in Unix/Linux with examples. Please try at your end and let me know if it helps you in any way. Please do provide the feedback.  If you like the  post, please like this page on Facebook

See also  Linux command for Oracle DBA

Related Articles

awk command
sed command
Useful Unix/Linux command for Oracle DBA
Grep equivalent in Windows
grep examples

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top