Syntax:
Basic examples
[b]1: List all files in current and sub directories[/b]
The command is same as the following
[b]2. Search specific directory or path[/b]
The following command will look for files in the downloads directory in the current directory. Lists out all files by default.
The following command searches for files by their name.
We can also use wildcards
Note that all sub directories 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.
[quote]
When specifying the directory ("./downloads" in this example), its fine to omit the trailing slash. However, if the directory is actually a symlink to some other location then you MUST specify the trailing slash for it to work properly (find ./downloads/ ...)[/quote]
[b]Ignore the case[/b]
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.
[quote]
Its always better to wrap the search term (name parameter) in double or single quotes. Not doing so will seem to work sometimes and give strange results at other times.[/quote]
[b]3. Limit depth of directory traversal[/b]
The find command by default travels down the entire directory tree recursively, which is time and resource consuming. However the depth of directory travesal can be specified. For example we don't want to go more than 2 or 3 levels down in the sub directories. This is done using the maxdepth option.
This is very useful when we want to do a limited search only in the current directory or max 1 level deep sub directories 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 atleast N level deep before searching for the files.
[b]4. Invert match[/b]
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.
So in the above example we found all files that do not have the extension of jar, either non-jar files. The find command also supports the exclamation mark inplace of not.
[quote]$ find ./downloads/ ! -name "*.jar"[/quote]
[b]5. Combine multiple search criterias[/b]
It is possible to use multiple criterias when specifying name and inverting. For example
[b]OR operator[/b]
When using multiple name criterias, the find command would combine them with AND operator, which means that only those files which satisfy all criterias will be matched. However if we need to perform an OR based matching then the find command has the "o" switch.
[b]6. Search only files or only directories[/b]
Sometimes we want to find only files or only directories with a given name. Find can do this easily as well.
Quite useful and handy!
[b]7. Search multiple directories together[/b]
So lets say you want to search inside 2 separate directories. Again, the command is very simple
[b]8. Find hidden files[/b]
Hidden files on linux begin with a period. So its easy to mention that in the name criteria and list all hidden files.
[size=x-large]Find files based on permissions[/size]
[b]9. Find files with certain permissions[/b]
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
[b]10. Find files with sgid/suid bits set[/b]
Note that the "2>/dev/null" removes those entries that have an error of "Permission Denied"
[b]11. Find readonly files[/b]
Find all Read Only files.
[b]12. Find executable files[/b]
The following command will find executable files
[b]Search Files Based On Owners and Groups[/b]
[b]13. Find files belonging to particular user[/b]
[b]14. Search files belonging to group[/b]
Find all files that belong to a particular group.
Did you know you could search your home directory by using the ~ symbol ?
[quote]$ find ~ -name "hidden.php"[/quote]
[b]Search file and directories based on modification date and time[/b]
Another great search criteria that the find command supports is 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. Lets take a few examples
[b]15. Find files modified N days back[/b]
To find all the files which are modified 50 days back.
[b]16. Find files accessed in last N days[/b]
Find all files that were accessed in the last 50 days.
[b]17. Find files modified in a range of days[/b]
Find all files that were modified between 2 to 10 days ago.
[b]18. Find files changed in last N minutes.[/b]
Find files modified within the last 1 hour.
[b]Search files and directories based on size[/b]
[b]21. Find files of given size[/b]
To find all 50MB files, use.
[b]22. Find files in a size range[/b]
To find all the files which are greater than 50MB and less than 100MB.
[b]23. Find largest and smallest files[/b]
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.
Similary when sorted in ascending order, it would show the smallest files first
[b]24. Find empty files and directories[/b]
The following command uses the "empty" option of the find command, which finds all files that are empty.
[b]25. List out the found files[/b]
Lets 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.
[b]26. Delete all matching files or directories[/b]
[url]http://www.binarytides.com/linux-find-command-examples/[/url]
$ find location comparison-criteria search-term
Basic examples
[b]1: List all files in current and sub directories[/b]
$ find
.
./.mozilla
./.mozilla/plugins
./.mozilla/extensions
./.bash_logout
./.kshrc
./.bash_profile
./.ssh
./.ssh/id_rsa
./.ssh/id_rsa.pub
./.ssh/known_hosts
./.bash_history
./downloads
./downloads/TeamCity-8.1.4.tar.gz
./downloads/nexus-2.8.1-01-bundle.tar.gz
./downloads/ojdbc6.jar
./downloads/httpd-2.4.16.tar.gz
./downloads/gcc-5.2.0.tar.gz
./downloads/bootstrap.jar
./downloads/fcm-core-1.1-SNAPSHOT-dist.tar.gz
./downloads/cfd-server-3.3.0-SNAPSHOT-dist.tar.gz
./.lesshst
./.bashrc
./.viminfo
./auth.zip
The command is same as the following
$ find .
$ find . -print
[b]2. Search specific directory or path[/b]
The following command will look for files in the downloads directory in the current directory. Lists out all files by default.
$ find ./downloads
./downloads
./downloads/TeamCity-8.1.4.tar.gz
./downloads/nexus-2.8.1-01-bundle.tar.gz
./downloads/ojdbc6.jar
./downloads/httpd-2.4.16.tar.gz
./downloads/gcc-5.2.0.tar.gz
./downloads/bootstrap.jar
./downloads/fcm-core-1.1-SNAPSHOT-dist.tar.gz
./downloads/cfd-server-3.3.0-SNAPSHOT-dist.tar.gz
The following command searches for files by their name.
$ find ./downloads -name "ojdbc6.jar"
./downloads/ojdbc6.jar
We can also use wildcards
$ find ./downloads -name "*.jar"
./downloads/ojdbc6.jar
./downloads/bootstrap.jar
Note that all sub directories 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.
[quote]
When specifying the directory ("./downloads" in this example), its fine to omit the trailing slash. However, if the directory is actually a symlink to some other location then you MUST specify the trailing slash for it to work properly (find ./downloads/ ...)[/quote]
[b]Ignore the case[/b]
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.
$ find ./downloads/ -iname "*.JAR"
./downloads/ojdbc6.jar
./downloads/bootstrap.jar
[quote]
Its always better to wrap the search term (name parameter) in double or single quotes. Not doing so will seem to work sometimes and give strange results at other times.[/quote]
[b]3. Limit depth of directory traversal[/b]
The find command by default travels down the entire directory tree recursively, which is time and resource consuming. However the depth of directory travesal can be specified. For example we don't want to go more than 2 or 3 levels down in the sub directories. This is done using the maxdepth option.
$ find ./ -maxdepth 1 -name "*.jar"
$ find ./ -maxdepth 2 -name "*.jar"
./downloads/ojdbc6.jar
./downloads/bootstrap.jar
This is very useful when we want to do a limited search only in the current directory or max 1 level deep sub directories 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 atleast N level deep before searching for the files.
[b]4. Invert match[/b]
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.
$ find ./downloads/ -not -name "*.jar"
./downloads/
./downloads/TeamCity-8.1.4.tar.gz
./downloads/nexus-2.8.1-01-bundle.tar.gz
./downloads/httpd-2.4.16.tar.gz
./downloads/gcc-5.2.0.tar.gz
./downloads/fcm-core-1.1-SNAPSHOT-dist.tar.gz
./downloads/cfd-server-3.3.0-SNAPSHOT-dist.tar.gz
So in the above example we found all files that do not have the extension of jar, either non-jar files. The find command also supports the exclamation mark inplace of not.
[quote]$ find ./downloads/ ! -name "*.jar"[/quote]
[b]5. Combine multiple search criterias[/b]
It is possible to use multiple criterias when specifying name and inverting. For example
$ find ./downloads/ -name "httpd*" ! -name "*.jar"
./downloads/httpd-2.4.16.tar.gz
[b]OR operator[/b]
When using multiple name criterias, the find command would combine them with AND operator, which means that only those files which satisfy all criterias will be matched. However if we need to perform an OR based matching then the find command has the "o" switch.
$ find ./downloads/ -name "httpd*" -o -name "*.jar"
./downloads/ojdbc6.jar
./downloads/httpd-2.4.16.tar.gz
./downloads/bootstrap.jar
[b]6. Search only files or only directories[/b]
Sometimes we want to find only files or only directories with a given name. Find can do this easily as well.
$ find . -name "downloads*"
./downloads
./downloads.txt
ONLY FILES
[devfcm@portal.ny1 ~]$ find . -type f -name "downloads*"
./downloads.txt
ONLY DIRECTORIES
[devfcm@portal.ny1 ~]$ find . -type d -name "downloads*"
./downloads
Quite useful and handy!
[b]7. Search multiple directories together[/b]
So lets say you want to search inside 2 separate directories. Again, the command is very simple
$ find ./downloads/ ./bakup/ -type f -name "ojdbc*"
./downloads/ojdbc6.jar
./bakup/ojdbc6.jar
[b]8. Find hidden files[/b]
Hidden files on linux begin with a period. So its easy to mention that in the name criteria and list all hidden files.
$ find ~ -type f -name ".*"
[size=x-large]Find files based on permissions[/size]
[b]9. Find files with certain permissions[/b]
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
$ find . -type f -perm 0664
./downloads/TeamCity-8.1.4.tar.gz
./downloads/httpd-2.4.16.tar.gz
./downloads/gcc-5.2.0.tar.gz
./auth.zip
./downloads.txt
$ find . -type f ! -perm 0777
./fcm-core/fcm-core-dev-1.1-SNAPSHOT.jar
./.viminfo
./derivConnectKeystore.jks
./tc-helpdesk.war
./loadEodData.sh
[b]10. Find files with sgid/suid bits set[/b]
$ find / -maxdepth 2 -perm /u=s 2>/dev/null
/bin/ping
/bin/mount
/bin/ping6
/bin/su
/bin/umount
/sbin/mount.nfs
/sbin/pam_timestamp_check
/sbin/unix_chkpwd
Note that the "2>/dev/null" removes those entries that have an error of "Permission Denied"
[b]11. Find readonly files[/b]
Find all Read Only files.
$ find /etc -maxdepth 1 -perm /u=r
/etc
/etc/system-release-cpe
/etc/chkconfig.d
[b]12. Find executable files[/b]
The following command will find executable files
$ find /bin/ -maxdepth 2 -perm /a=x
/bin/
/bin/ex
/bin/tracepath
[b]Search Files Based On Owners and Groups[/b]
[b]13. Find files belonging to particular user[/b]
$ find . -user zxin -name "*test*" 2>/dev/null
./test.sh
./OCRTest/prod-test
./logrotate/testlog.txt
[b]14. Search files belonging to group[/b]
Find all files that belong to a particular group.
find ~ -group calyp
/home/staff/clu
/home/staff/clu/.mozilla
/home/staff/clu/.mozilla/plugins
/home/staff/clu/.mozilla/extensions
/home/staff/clu/.bash_logout
Did you know you could search your home directory by using the ~ symbol ?
[quote]$ find ~ -name "hidden.php"[/quote]
[b]Search file and directories based on modification date and time[/b]
Another great search criteria that the find command supports is 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. Lets take a few examples
[b]15. Find files modified N days back[/b]
To find all the files which are modified 50 days back.
查找60天以来都没有修改过的文件
$ find ./ -mtime +60
./.mozilla
./.mozilla/plugins
./.mozilla/extensions
./.bash_logout
./.kshrc
./.bashrc
./.bash_profile
查找近60天被修改过的文件
$ find ./ -mtime -60
./
./fcm-core
./fcm-core/fcm-core-dev-1.1-SNAPSHOT.jar
./.bash_history
./derivConnectKeystore.jks
[b]16. Find files accessed in last N days[/b]
Find all files that were accessed in the last 50 days.
atime的意思是access time,即文件的最近的一次访问时间,+n意思为查找n天以前的文件,-n为查找n天以内的文件
$ find ./ -atime -2
[b]17. Find files modified in a range of days[/b]
Find all files that were modified between 2 to 10 days ago.
查找修改时间在2天以前10天以内的文件
$ find ./ -mtime +2 -mtime -10
./fcm-core-dev-1.1-SNAPSHOT-dist.tar.gz
[b]18. Find files changed in last N minutes.[/b]
Find files modified within the last 1 hour.
$ find ./ -cmin -60
./
./loadEodData.sh
[b]Search files and directories based on size[/b]
[b]21. Find files of given size[/b]
To find all 50MB files, use.
$ find ./ -size 50M
[b]22. Find files in a size range[/b]
To find all the files which are greater than 50MB and less than 100MB.
$ find ./ -size +50M -size -100M
[b]23. Find largest and smallest files[/b]
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.
$ find . -type f -exec ls -s {} \; | sort -n -r | head -5
2302744 ./oom.hprof
79860 ./fcm-core-dev-1.1-SNAPSHOT-dist.tar.gz
57164 ./fcm-core/fcm-eurex-convertion-0.0.1-SNAPSHOT-dist.tar.gz
53816 ./fcm-eurex-convertion/fcm-eurex-conversion-0.0.1-SNAPSHOT-dist.tar.gz
53816 ./fcm-eurex-convertion/20141016/fcm-eurex-conversion-0.0.1-SNAPSHOT-dist.tar.gz
Similary when sorted in ascending order, it would show the smallest files first
$ find . -type f -exec ls -s {} \; | sort -n | head -5
[b]24. Find empty files and directories[/b]
The following command uses the "empty" option of the find command, which finds all files that are empty.
$ find ./ -type f -empty
$ find ./ -type d -empty
[b]25. List out the found files[/b]
Lets 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.
$ find ./ -size +50M -exec ls -ld {} \;
[b]26. Delete all matching files or directories[/b]
$ find /tmp -type f -name "*.txt" -exec rm -f {} \;
[url]http://www.binarytides.com/linux-find-command-examples/[/url]