- 浏览: 162569 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
mengyue0477:
最后还不忘吐槽一下 哈哈 太逗了
spring容器的懒加载lazy-init设置 -
comet0515:
不是maven工程能不能用checkstyle插件?job是一 ...
jenkins 配置checkstyle -
xfxlch:
http://docs.spring.io/spring-bo ...
NoteBook -
With_Me_Forever:
太浅了。我最近也在看着方面的资料,一起进步把。
互联网金融定义 -
xfxlch:
在http://r.m.baidu.com/6pmxgig
NoteBook
Syntax:
Basic examples
1: List all files in current and sub directories
The command is same as the following
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.
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.
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.
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.
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.
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.
5. Combine multiple search criterias
It is possible to use multiple criterias when specifying name and inverting. For example
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.
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.
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
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 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
10. Find files with sgid/suid bits set
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.
12. Find executable files
The following command will find executable files
Search Files Based On Owners and Groups
13. Find files belonging to particular user
14. Search files belonging to group
Find all files that belong to a particular group.
Did you know you could search your home directory by using the ~ symbol ?
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.
16. Find files accessed in last N days
Find all files that were accessed in the last 50 days.
17. Find files modified in a range of days
Find all files that were modified between 2 to 10 days ago.
18. Find files changed in last N minutes.
Find files modified within the last 1 hour.
Search files and directories based on size
21. Find files of given size
To find all 50MB files, use.
22. Find files in a size range
To find all the files which are greater than 50MB and less than 100MB.
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.
Similary when sorted in ascending order, it would show the smallest files first
24. Find empty files and directories
The following command uses the "empty" option of the find command, which finds all files that are 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.
26. Delete all matching files or directories
http://www.binarytides.com/linux-find-command-examples/
$ 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 命令之sed
2015-12-24 14:06 0sed是是简写: sed, short for "s ... -
wget/mget,scp/rcp的区别
2015-11-16 09:38 0linux命令 -
后台运行bash
2015-08-25 12:50 420背景: 在Linux里,当你运行一个shell脚本的时候,就会 ... -
Hack 12. Change the Case
2015-05-27 22:28 4511. 把一个文件中的字母全部format成大写: tr a-z ... -
Hack 11. Join Command
2015-05-26 03:36 600jion 命令用来combine连个文件,前提是这两个文件有共 ... -
Hack 10. Suppress Standard Output and Error Message
2015-05-24 23:02 416对于这一条使用技巧,在日常操作中使用的频率并不大。 使用到的场 ... -
Hack 9. Find Command
2015-05-23 23:25 596有趣的FIND命令来了。 find命令是用来查找文件用的,这里 ... -
Hack 8. Regular Expression in Grep
2015-05-23 00:26 498grep 命令中表达式的应用。正则表达式在我们编程语言和lin ... -
Hack 7. Grep Command
2015-05-20 20:41 537grep 命令在我们的日常操作中应用的很普遍。很多时候我们都会 ... -
Hack 6. Automatically Correct Mistyped Directory Names
2015-05-19 18:56 401[clu@portal.ny1 mail]$ pwd /tm ... -
Hack 5. Manipulate Directory Stack
2015-05-18 23:35 481该组命令在日常应用中用到的应该不多。 这组命令可以用来把多个目 ... -
Hack 4. Toggle Between Directories
2015-05-17 23:04 425cd - 命令是在linux中在最近的两个目录之间实现快速切换 ... -
Hack 3. Perform mkdir and cd Using a Single Command
2015-05-17 16:00 428在linux系统中,我们经常会利用mkdir命令创建目录,并且 ... -
Hack 2. Use CD Alias to Navigate Up the Directory
2015-05-16 00:33 522cd ..命令可以返回到上一层目录。 我们都知道使用cd .. ... -
Hack 1. Define CD Base Directory Using CDPATH
2015-05-14 17:06 437CDPATH:是linux系统中预留的一个环境变量,该变量不可 ... -
Shell
2014-07-21 18:40 0#Reading white list file_input= ... -
vi 退出保持命令
2014-07-14 19:03 570保存命令 按ESC键 跳到命令模式,然后: :w 保存 ...
相关推荐
主要为大家详细介绍了linux命令之find命令的简单使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
linux find 命令大全面linux find 命令大全
Linux下find命令在目录结构中搜索文件,并执行指定的操作。Linux下find命令提供了相当多的查找条件,功能很强大。由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统...
linux 命令 find linux 命令 find linux 命令 find linux 命令 find linux 命令 find linux 命令 find
Linux运维实用命令之find详解,find命令用于Linux查询最实用命令之一,请大家参考。
如果你想在linux系统的命令行中变得特别高效,那么 find 是你必须掌握的命令之一。 find 命令的基本语法如下: $ find [path] [option] [expression] 一、基本用法 1. 列出当前目录和子目录下的所有文件 这个命令...
Linux文件查找命令find xargs
linux常用的find命令整理,希望对初学者有帮助!
Linux Find 命令详解 非常详尽
linux下的find命令格式及参数。
find命令是linux下比较常用的命令之一,作用较大。linux find命令精通指南
详细介绍了linux下的find命令经常使用的选项的意义和方法
This manual page documents the GNU version of find. GNU find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of...
linux find命令说明
linux 下 find 命令的用法
文章主要对Linux文件查找命令find和xargs进行详细描述,感性趣的朋友可以参考下。
超级详细的Linux,find命令详解,很实用适合Linux学习爱好者!
自己整理的Linux系统下find命令的常用语法
linux下 find命令实例,txt命令实例