`
xfxlch
  • 浏览: 168065 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Linux 命令之find

阅读更多
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命令使用手册

    ### Linux Find 命令使用手册详解 #### 概述 `find` 是一个功能强大的命令行工具,用于在Linux系统中的目录树中查找文件。它支持多种选项和表达式来帮助用户根据特定条件筛选出目标文件。此文档将详细介绍`find`...

    使用find命令查找Linux中的隐藏文件的方法.docx

    find命令是Linux和Unix系统中最强大和灵活的命令之一,能够根据不同的选项和参数来实现各种文件搜索和管理任务。下面将详细介绍如何使用find命令查找Linux中的隐藏文件。 一、基本语法 find命令的基本语法为:find...

    Linux Find命令详解---教你认识强大的Linux Find命令

    Linux Find命令是Linux系统中一个极其重要的工具,它允许用户在文件系统中查找符合特定条件的文件和目录。这个命令的灵活性和强大性使得它成为系统管理员和开发者的必备技能。下面将详细介绍Linux Find命令的一些...

    Linux中find命令的用法汇总

    如果你想在linux系统的命令行中变得特别高效,那么 find 是你必须掌握的命令之一。 find 命令的基本语法如下: $ find [path] [option] [expression] 一、基本用法 1. 列出当前目录和子目录下的所有文件 这个命令...

    linux查找文件命令find.docx

    Find 命令是 Linux 中最常用的查找文件命令,对于 Linux 用户来说,它是必备的技能之一。 知识点: 1. Find 命令的基本语法是:find [路径] [选项] [操作] 2. 通过文件名查找法:find / -name d.conf 3. 根据文件...

    linux命令find实现_find.zip

    linux命令find实现_find

    Linux文件查找命令之find讲解.doc

    Linux下的`find`命令是系统管理员和用户们日常工作中不可或缺的工具,它允许用户在文件系统中高效地查找和管理文件。这篇文档将详细介绍`find`命令的基本使用、选项、参数以及一些实用的实例。 `find`命令的基本...

    linux下find命令的用法

    "linux下find命令的用法" Linux 操作系统下的文件查找命令是 find 命令,这个命令可以帮助用户在 Linux 系统中快速查找需要的文件。find 命令的使用方法非常多样化,可以根据文件名、文件大小、文件类型、修改时间...

    linux下find命令

    Linux 下 find 命令详解 find 命令是 Linux 系统中一个功能强大且常用的命令,用于在文件系统中搜索文件。它可以根据文件的各种属性,如名称、权限、所有者、组、时间戳、大小等,来查找指定的文件。 基本语法 ...

    Linux文件查找命令find

    ### Linux 文件查找命令 find 在Linux系统中,`find`命令是极其强大的工具,用于在文件系统中搜索和定位特定的文件或目录。通过灵活的选项设置,`find`能够根据文件名、权限、所有者、修改时间等多种条件进行精确...

    Linux文件查找命令find,xargs详述

    Linux文件查找命令`find`和`xargs`是Linux系统中非常重要的工具,它们帮助用户在文件系统中高效地定位和处理文件。本文将详细解释这两个命令的基本使用和相关选项。 `find`命令是一个功能强大的命令,用于在指定...

    Linux实现类似find命令的myfind

    仿照unix操作系统中的find命令,在实现一个myfind命令。myfind命令从指定的目录下开始,递归地查找指定文件

    linux find 命令大全

    ### Linux Find 命令详解 #### 概述 `find` 命令是 Linux 系统中一个非常强大的工具,用于在指定路径下查找文件。它支持多种选项来帮助用户精确地定位到需要的文件。通过 `find` 命令,用户可以基于文件名、文件...

    linux下find命令查找排除命令(简单明了)

    通过上述示例,我们可以看到`find`命令的强大之处不仅在于它能够帮助我们在复杂的文件系统中快速定位文件,还在于它能够灵活地排除不需要搜索的路径。这对于维护大型项目或清理不必要的文件特别有用。需要注意的是,...

    linux查找文件命令find

    Linux 查找文件命令 find Linux 操作系统中查找文件的命令是 find 命令,这个命令可以...find 命令是一个非常强大的命令,能够帮助用户快速地查找、删除、复制、移动文件等操作,是 Linux 系统中必不可少的命令之一。

    Linux 命令速查手册

    Linux命令速查手册作为一本全面介绍Linux命令的参考书籍,由美国作者Scott Granneman所著,并由成保栋与李保强翻译成中文版本。本书是图灵系统与网络管理技术丛书之一,适合所有与Linux相关的工作人员以及初学者,...

    Linux命令壁纸带中文.rar

    为了更好地理解和记忆Linux命令,本资源提供了一套“Linux命令壁纸带中文”的学习资料。这些壁纸将常见的Linux命令与精美的图像结合,每张壁纸上都清晰地标注了对应的中文注释,旨在帮助用户在日常使用中加深对命令...

Global site tag (gtag.js) - Google Analytics