- 浏览: 167444 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
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 462背景: 在Linux里,当你运行一个shell脚本的时候,就会 ... -
Hack 12. Change the Case
2015-05-27 22:28 4851. 把一个文件中的字母全部format成大写: tr a-z ... -
Hack 11. Join Command
2015-05-26 03:36 641jion 命令用来combine连个文件,前提是这两个文件有共 ... -
Hack 10. Suppress Standard Output and Error Message
2015-05-24 23:02 462对于这一条使用技巧,在日常操作中使用的频率并不大。 使用到的场 ... -
Hack 9. Find Command
2015-05-23 23:25 652有趣的FIND命令来了。 find命令是用来查找文件用的,这里 ... -
Hack 8. Regular Expression in Grep
2015-05-23 00:26 531grep 命令中表达式的应用。正则表达式在我们编程语言和lin ... -
Hack 7. Grep Command
2015-05-20 20:41 568grep 命令在我们的日常操作中应用的很普遍。很多时候我们都会 ... -
Hack 6. Automatically Correct Mistyped Directory Names
2015-05-19 18:56 433[clu@portal.ny1 mail]$ pwd /tm ... -
Hack 5. Manipulate Directory Stack
2015-05-18 23:35 516该组命令在日常应用中用到的应该不多。 这组命令可以用来把多个目 ... -
Hack 4. Toggle Between Directories
2015-05-17 23:04 463cd - 命令是在linux中在最近的两个目录之间实现快速切换 ... -
Hack 3. Perform mkdir and cd Using a Single Command
2015-05-17 16:00 460在linux系统中,我们经常会利用mkdir命令创建目录,并且 ... -
Hack 2. Use CD Alias to Navigate Up the Directory
2015-05-16 00:33 557cd ..命令可以返回到上一层目录。 我们都知道使用cd .. ... -
Hack 1. Define CD Base Directory Using CDPATH
2015-05-14 17:06 480CDPATH:是linux系统中预留的一个环境变量,该变量不可 ... -
Shell
2014-07-21 18:40 0#Reading white list file_input= ... -
vi 退出保持命令
2014-07-14 19:03 607保存命令 按ESC键 跳到命令模式,然后: :w 保存 ...
相关推荐
### Linux Find 命令使用手册详解 #### 概述 `find` 是一个功能强大的命令行工具,用于在Linux系统中的目录树中查找文件。它支持多种选项和表达式来帮助用户根据特定条件筛选出目标文件。此文档将详细介绍`find`...
find命令是Linux和Unix系统中最强大和灵活的命令之一,能够根据不同的选项和参数来实现各种文件搜索和管理任务。下面将详细介绍如何使用find命令查找Linux中的隐藏文件。 一、基本语法 find命令的基本语法为:find...
Linux Find命令是Linux系统中一个极其重要的工具,它允许用户在文件系统中查找符合特定条件的文件和目录。这个命令的灵活性和强大性使得它成为系统管理员和开发者的必备技能。下面将详细介绍Linux Find命令的一些...
如果你想在linux系统的命令行中变得特别高效,那么 find 是你必须掌握的命令之一。 find 命令的基本语法如下: $ find [path] [option] [expression] 一、基本用法 1. 列出当前目录和子目录下的所有文件 这个命令...
Find 命令是 Linux 中最常用的查找文件命令,对于 Linux 用户来说,它是必备的技能之一。 知识点: 1. Find 命令的基本语法是:find [路径] [选项] [操作] 2. 通过文件名查找法:find / -name d.conf 3. 根据文件...
linux命令find实现_find
Linux下的`find`命令是系统管理员和用户们日常工作中不可或缺的工具,它允许用户在文件系统中高效地查找和管理文件。这篇文档将详细介绍`find`命令的基本使用、选项、参数以及一些实用的实例。 `find`命令的基本...
"linux下find命令的用法" Linux 操作系统下的文件查找命令是 find 命令,这个命令可以帮助用户在 Linux 系统中快速查找需要的文件。find 命令的使用方法非常多样化,可以根据文件名、文件大小、文件类型、修改时间...
Linux 下 find 命令详解 find 命令是 Linux 系统中一个功能强大且常用的命令,用于在文件系统中搜索文件。它可以根据文件的各种属性,如名称、权限、所有者、组、时间戳、大小等,来查找指定的文件。 基本语法 ...
### Linux 文件查找命令 find 在Linux系统中,`find`命令是极其强大的工具,用于在文件系统中搜索和定位特定的文件或目录。通过灵活的选项设置,`find`能够根据文件名、权限、所有者、修改时间等多种条件进行精确...
Linux文件查找命令`find`和`xargs`是Linux系统中非常重要的工具,它们帮助用户在文件系统中高效地定位和处理文件。本文将详细解释这两个命令的基本使用和相关选项。 `find`命令是一个功能强大的命令,用于在指定...
仿照unix操作系统中的find命令,在实现一个myfind命令。myfind命令从指定的目录下开始,递归地查找指定文件
### Linux Find 命令详解 #### 概述 `find` 命令是 Linux 系统中一个非常强大的工具,用于在指定路径下查找文件。它支持多种选项来帮助用户精确地定位到需要的文件。通过 `find` 命令,用户可以基于文件名、文件...
通过上述示例,我们可以看到`find`命令的强大之处不仅在于它能够帮助我们在复杂的文件系统中快速定位文件,还在于它能够灵活地排除不需要搜索的路径。这对于维护大型项目或清理不必要的文件特别有用。需要注意的是,...
Linux 查找文件命令 find Linux 操作系统中查找文件的命令是 find 命令,这个命令可以...find 命令是一个非常强大的命令,能够帮助用户快速地查找、删除、复制、移动文件等操作,是 Linux 系统中必不可少的命令之一。
Linux命令速查手册作为一本全面介绍Linux命令的参考书籍,由美国作者Scott Granneman所著,并由成保栋与李保强翻译成中文版本。本书是图灵系统与网络管理技术丛书之一,适合所有与Linux相关的工作人员以及初学者,...
为了更好地理解和记忆Linux命令,本资源提供了一套“Linux命令壁纸带中文”的学习资料。这些壁纸将常见的Linux命令与精美的图像结合,每张壁纸上都清晰地标注了对应的中文注释,旨在帮助用户在日常使用中加深对命令...