"To be a warrior is not a simple matter of wishing to be one. It is rather an endless struggle that will go on to the very last moment of our lives. Nobody is born a warrior, in exactly the same way that nobody is born an average man. We make ourselves into one or the other." --Kokoro
(Copied all these from https://www.binarytides.com/linux-find-command-examples/)
1. List all files in current and subdirectories
This command lists out all the files in the current directory as well as the subdirectories in the current directory.
1
2
3
4
5
$find.
./abc.txt
./subdir
./subdir/how.php
./cool.php
The command is same as the following
1
2
$find.
$find.-print
2. Search specific directory or path
The following command will look for files in the test directory in the current directory. Lists out all files by default.
1
2
3
4
5
6
$find./test
./test
./test/abc.txt
./test/subdir
./test/subdir/how.php
./test/cool.php
The following command searches for files by their name.
1
2
$find./test-name"abc.txt"
./test/abc.txt
We can also use wildcards
1
2
3
$find./test-name"*.php"
./test/subdir/how.php
./test/cool.php
Note that all subdirectories are searched recursively. So this is a very powerful way to find all files of a given extension.
Trying to search the “/” directory which is the root, would search the entire file system including mounted devices and network storage devices. So be careful. Of course, you can press Ctrl + c anytime to stop the command.
1
When specifying the directory("./test"inthisexample),its fine toomit the trailing slash.However,ifthe directory isactuallyasymlink tosome other location thenyou MUST specify the trailing slash forit towork properly(find./test/...)
Ignore the case
It is often useful to ignore the case when searching for file names. To ignore the case, just use the “iname” option instead of the “name” option.
1
2
3
$find./test-iname"*.Php"
./test/subdir/how.php
./test/cool.php
1
Its always better towrap the search term(name parameter)indoubleorsingle quotes.Notdoing so will seem towork sometimes andgive strange results at other times.
3. Limit depth of directory traversal
The find command by default travels down the entire directory tree recursively, which is time and resource consuming. However, the depth of directory traversal can be specified. For example, we don’t want to go more than 2 or 3 levels down in the subdirectories. This is done using the maxdepth option.
1
2
3
4
5
6
$find./test-maxdepth2-name"*.php"
./test/subdir/how.php
./test/cool.php
$find./test-maxdepth1-name *.php
./test/cool.php
The second example uses maxdepth of 1, which means it will not go lower than 1 level deep, either only in the current directory.
This is very useful when we want to do a limited search only in the current directory or max 1 level deep subdirectories and not the entire directory tree which would take more time.
Just like maxdepth there is an option called mindepth which does what the name suggests, that is, it will go at least N level deep before searching for the files.
4. Invert match
It is also possible to search for files that do no match a given name or pattern. This is helpful when we know which files to exclude from the search.
1
2
3
4
$find./test-not-name"*.php"
./test
./test/abc.txt
./test/subdir
So in the above example, we found all files that do not have the extension of php, either non-php files. The find command also supports the exclamation mark inplace of not.
1
find./test!-name"*.php"
5. Combine multiple search criteria
It is possible to use multiple criteria when specifying name and inverting. For example
1
2
3
$find./test-name'abc*'!-name'*.php'
./test/abc.txt
./test/abc
The above find command looks for files that begin with abc in their names and do not have a php extension. This is an example of how powerful search expressions can be built with the find command.
OR operator
When using multiple name criteria, the find command would combine them with AND operator, which means that only those files which satisfy all criteria will be matched. However, if we need to perform an OR based matching then the find command has the “o” switch.
1
2
3
4
5
$find-name'*.php'-o-name'*.txt'
./abc.txt
./subdir/how.php
./abc.php
./cool.php
The above command search for files ending in either the php extension or the txt extension.
6. Search only files or only directories
Sometimes we want to find only files or only directories with a given name. Find can do this easily as well.
1
2
3
4
5
6
7
8
9
10
11
12
13
$find./test-name abc*
./test/abc.txt
./test/abc
Only files
$find./test-typef-name"abc*"
./test/abc.txt
Only directories
$find./test-typed-name"abc*"
./test/abc
Quite useful and handy!
7. Search multiple directories together
So let’s say you want to search inside 2 separate directories. Again, the command is very simple
1
2
3
$find./test./dir2-typef-name"abc*"
./test/abc.txt
./dir2/abcdefg.txt
Check, that it listed files from 2 separate directories.
8. Find hidden files
Hidden files on Linux begin with a period. So its easy to mention that in the name criteria and list all hidden files.
1
$find~-typef-name".*"
9. Find files with certain permissions
The find command can be used to find files with a specific permission using the “perm” option. The following command searches for files with the permission 0664
1
2
3
4
5
$find.-typef-perm0664
./abc.txt
./subdir/how.php
./abc.php
./cool.php
This can be useful to find files with wrong permissions which can lead to security issues. Inversion can also be applied to permission checking.
1
2
3
4
5
$find.-typef!-perm0777
./abc.txt
./subdir/how.php
./abc.php
./cool.php
10. Find files with sgid/suid bits set
The “perm” option of find command accepts the same mode string like chmod. The following command finds all files with permission 644 and sgid bit set.
1
# find / -perm 2644
Similarly, use 1664 for sticky bit. The perm option also supports using an alternative syntax instead of octal numbers.
1
2
3
4
5
6
7
8
$find/-maxdepth2-perm/u=s2>/dev/null
/bin/mount
/bin/su
/bin/ping6
/bin/fusermount
/bin/ping
/bin/umount
/sbin/mount.ecryptfs_private
Note that the “2>/dev/null” removes those entries that have an error of “Permission Denied”
11. Find readonly files
Find all Read Only files.
1
2
3
4
5
6
7
$find/etc-maxdepth1-perm/u=r
/etc
/etc/thunderbird
/etc/brltty
/etc/dkms
/etc/phpmyadmin
...output truncated...
12. Find executable files
The following command will find executable files
1
2
3
4
5
6
7
$find/bin-maxdepth2-perm/a=x
/bin
/bin/preseed_command
/bin/mount
/bin/zfgrep
/bin/tempfile
...output truncated...
13. Find files owned by particular user
To find all or single file called tecmint.txt under /root directory of owner root.
1
2
3
4
5
6
7
$find.-user bob
.
./abc.txt
./abc
./subdir
./subdir/how.php
./abc.php
We could also specify the name of the file or any name related criteria along with user criteria
1
$find.-user bob-name'*.php'
It’s very easy to see, how we can build up criteria after criteria to narrow down our search for matching files.
14. Search files belonging to group
Find all files that belong to a particular group.
1
# find /var/www -group developer
Did you know you could search your home directory by using the ~ symbol?
1
$find~-name"hidden.php"
Easy!!
Search file and directories based on modification date and time
Another great search criteria that the find command supports is a modification and accessed date/times. This is very handy when we want to find out which files were modified as a certain time or date range. Let’s take a few examples
15. Find files modified N days back
To find all the files which are modified 50 days back.
1
# find / -mtime 50
16. Find files accessed in last N days
Find all files that were accessed in the last 50 days.
1
# find / -atime 50
17. Find files modified in a range of days
Find all files that were modified between 50 to 100 days ago.
1
# find / -mtime +50 –mtime -100
18. Find files changed in last N minutes.
Find files modified within the last 1 hour.
1
$find/home/bob-cmin-60
19. Files modified in last hour
To find all the files which are modified in last 1 hour.
1
# find / -mmin -60
20. Find Accessed Files in Last 1 Hour
To find all the files which are accessed in last 1 hour.
1
# find / -amin -60
21. Find files of given size
Search files and directories based on size. To find all 50MB files, use.
1
# find / -size 50M
22. Find files in a size range
To find all the files which are greater than 50MB and less than 100MB.
1
$find/-size+50M-size-100M
23. Find largest and smallest files
The find command, when used in combination with the ls and sort command, can be used to list out the largest files.
The following command will display the 5 largest file in the current directory and its subdirectory. This may take a while to execute depending on the total number of files the command has to process.
1
$find.-typef-execls-s{}\;|sort-n-r|head-5
Similarly when sorted in ascending order, it would show the smallest files first
1
$find.-typef-execls-s{}\;|sort-n|head-5
24. Find empty files and directories
The following command uses the “empty” option of the find command, which finds all files that are empty.
1
# find /tmp -type f -empty
To file all empty directories use the type “d”.
1
$find~/-typed-empty
Really very simple and easy
Some advanced operations
The find command not only finds files based on certain criteria, it can also act upon those files using any Linux command. For example, we might want to delete some files.
Here are some quick examples
25. List out the found files
Let’s say we found files using find command, and now want to list them out as the ls command would have done. This is very easy.
(pulled from http://www.folkstalk.com/2011/12/101-examples-of-using-find-command-in.html)
Find Command in Unix and Linux Examples
Find is one of the powerful utility of Unix (or Linux)Â used for searching the files in a directory hierarchy. The syntax of find command is
1
find[pathnames][conditions]
Let see some practical exercises on using find command.
1. How to run the last executed find command?
1
!find
This will execute the last find command. It also displays the last find command executed along with the result on the terminal.
2. How to find for a file using name?
1
2
3
find-name"sum.java"
./bkp/sum.java
./sum.java
This will find all the files with name “sum.java” in the current directory and sub-directories.
3. How to find for files using name and ignoring case?
1
2
3
4
find-iname"sum.java"
./SUM.java
./bkp/sum.java
./sum.java
This will find all the files with name “sum.java” while ignoring the case in the current directory and sub-directories.
4. How to find for a file in the current directory only?
1
2
find-maxdepth1-name"sum.java"
./sum.java
This will find for the file “sum.java” in the current directory only
5. How to find for files containing a specific word in its name?
1
2
3
4
5
find-name"*java*"
./SUM.java
./bkp/sum.java
./sum.java
./multiply.java
It displayed all the files which have the word “java” in the filename
6. How to find for files in a specific directory?
1
find/etc-name"*java*"
This will look for the files in the /etc directory with “java” in the filename
7. How to find the files whose name are not “sum.java”?
1
2
3
4
5
find-not-name"sum.java"
.
./SUM.java
./bkp
./multiply.java
This is like inverting the match. It prints all the files except the given file “sum.java”.
8. How to limit the file searches to specific directories?
1
2
3
4
5
6
7
find-name"sum.java"
./tmp/sum.java
./bkp/var/tmp/files/sum.java
./bkp/var/tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java
./sum.java
You can see here the find command displayed all the files with name “sum.java” in the current directory and sub-directories.
a. How to print the files in the current directory and one level down to the current directory?
1
2
3
4
find-maxdepth2-name"sum.java"
./tmp/sum.java
./bkp/sum.java
./sum.java
b. How to print the files in the current directory and two levels down to the current directory?
1
2
3
4
5
find-maxdepth3-name"sum.java"
./tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java
./sum.java
c. How to print the files in the subdirectories between level 1 and 4?
1
2
3
4
5
6
find-mindepth2-maxdepth5-name"sum.java"
./tmp/sum.java
./bkp/var/tmp/files/sum.java
./bkp/var/tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java
9. How to find the empty files in a directory?
1
2
find.-maxdepth1-empty
./empty_file
10. How to find the largest file in the current directory and sub directories
1
find.-typef-execls-s{}\;|sort-n-r|head-1
The find command “find . -type f -exec ls -s {} \;” will list all the files along with the size of the file. Then the sort command will sort the files based on the size. The head command will pick only the first line from the output of sort.
11. How to find the smallest file in the current directory and sub directories
1
find.-typef-execls-s{}\;|sort-n-r|tail-1
Another method using find is
1
find.-typef-execls-s{}\;|sort-n|head-1
12. How to find files based on the file type?
a. Finding socket files
1
find.-types
b. Finding directories
1
find.-typed
c. Finding hidden directories
1
find-typed-name".*"
d. Finding regular files
1
find.-typef
e. Finding hidden files
1
find.-typef-name".*"
13. How to find files based on the size?
a. Finding files whose size is exactly 10M
1
find.-size10M
b. Finding files larger than 10M size
1
find.-size+10M
c. Finding files smaller than 10M size
1
find.-size-10M
14. How to find the files which are modified after the modification of a give file.
1
find-newer"sum.java"
This will display all the files which are modified after the file “sum.java”
15. Display the files which are accessed after the modification of a give file.
1
find-anewer"sum.java"
16. Display the files which are changed after the modification of a give file.
1
find-cnewer"sum.java"
17. How to find the files based on the file permissions?
1
find.-perm777
This will display the files which have read, write, and execute permissions. To know the permissions of files and directories use the command “ls -l”.
18. Find the files which are modified within 30 minutes.
1
find.-mmin-30
19. Find the files which are modified within 1 day.
1
find.-mtime-1
20. How to find the files which are modified 30 minutes back
1
find.-not-mmin-30
21. How to find the files which are modified 1 day back.
1
find.-not-mtime-1
22. Print the files which are accessed within 1 hour.
1
find.-amin-60
23. Print the files which are accessed within 1 day.
1
find.-atime-1
24. Display the files which are changed within 2 hours.
1
find.-cmin-120
25. Display the files which are changed within 2 days.
1
find.-ctime-2
26. How to find the files which are created between two files.
1
find.-cnewer f1-and!-cnewer f2
So far we have just find the files and displayed on the terminal. Now we will see how to perform some operations on the files.
27. How to find the permissions of the files which contain the name “java”?
1
find-name"*java*"|xargs ls-l
Alternate method is
1
find-name"*java*"-execls-l{}\;
28.  Find the files which have the name “java” in it and then display only the files which have “class” word in them?
1
find-name"*java*"-execgrep-Hclass{}\;
29. How to remove files which contain the name “java”.