欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux 命令之find

程序员文章站 2022-07-13 08:18:09
...
Syntax:
$ find location comparison-criteria search-term


Basic examples
1: List all files in current and sub directories
$ 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


2. Search specific directory or path
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.

引用

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/ ...)


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.
$ find ./downloads/ -iname "*.JAR"
./downloads/ojdbc6.jar
./downloads/bootstrap.jar


引用

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.


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 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.

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.
$ 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.
引用
$ find ./downloads/ ! -name "*.jar"


5. Combine multiple search criterias
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


OR operator
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


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.
$ 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!

7. Search multiple directories together
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


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.
$ find ~ -type f -name ".*"


Find files based on permissions
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
$ 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


10. Find files with sgid/suid bits set
$ 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"

11. Find readonly files
Find all Read Only files.
$ find /etc -maxdepth 1 -perm /u=r
/etc
/etc/system-release-cpe
/etc/chkconfig.d


12. Find executable files

The following command will find executable files
$ find /bin/ -maxdepth 2 -perm /a=x
/bin/
/bin/ex
/bin/tracepath


Search Files Based On Owners and Groups

13. Find files belonging to particular user
$ find . -user zxin -name "*test*" 2>/dev/null
./test.sh
./OCRTest/prod-test
./logrotate/testlog.txt


14. Search files belonging to group
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 ?
引用
$ find ~ -name "hidden.php"


Search file and directories based on modification date and time

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

15. Find files modified N days back
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


16. Find files accessed in last N days
Find all files that were accessed in the last 50 days.
atime的意思是access time,即文件的最近的一次访问时间,+n意思为查找n天以前的文件,-n为查找n天以内的文件
$ find ./ -atime -2


17. Find files modified in a range of days

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


18. Find files changed in last N minutes.
Find files modified within the last 1 hour.
$ find ./ -cmin -60
./
./loadEodData.sh


Search files and directories based on size

21. Find files of given size
To find all 50MB files, use.
$ find ./ -size 50M


22. Find files in a size range
To find all the files which are greater than 50MB and less than 100MB.
$ 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.
$ 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


24. Find empty files and directories
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



25. List out the found files

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 {} \;


26. Delete all matching files or directories

$ find /tmp -type f -name "*.txt" -exec rm -f {} \;



http://www.binarytides.com/linux-find-command-examples/
相关标签: linux find