Linux find命令是命令行上很是有用和方便的搜索文件的命令。它可用于根据各类搜索标准查找文件,如权限,用户全部权,修改时间/日期,大小等。在这篇文章中,咱们将学习使用find命令以及它支持的各类选项。php
大多数Linux发行版find命令默认是可用的,所以你没必要安装任何软件包。在Linux上,若是你想高效的使用Linux命令行,find命令是必须学习的。linux
基本的find命令语法以下安全
$ find location comparison-criteria search-term
这条命令列出当前目录中的全部文件以及当前目录中的子目录。网络
# find . ./abc.txt ./cool.php ./subdir ./subdir/how.php
这条命令与如下命令等价ide
# find . # find . -print
如下命令将查找当前目录中test目录下的文件,默认状况下将列出全部文件。学习
# find ./test ./test ./test/abc.txt ./test/cool.php ./test/subdir ./test/subdir/how.php
如下命令经过文件名搜索文件ui
# find ./test -name "abc.txt" ./test/abc.txt
咱们也可使用通配符lua
# find ./test -name "*.php" ./test/cool.php ./test/subdir/how.php
请注意,因此有的子目录都是被递归搜索的。所以,这是查找给定扩展名全部文件的一种很是强大的方法。命令行
尝试搜索根目录"/",将会搜索整个文件系统包括挂载设备和网络存储设备。 因此要当心,固然大家能够随时经过Ctrl + c停止命令。unix
当指定目录时(本例中为"./test"),能够省略尾部的斜线,然而,若是目录是指向其余位置的连接时,为了使它工做正常,你必须指定尾部的斜线(find ./test/ ...)
忽略大小写
当搜索文件名时忽略大小写一般颇有用。为了忽略大小写,只需使用"iname"选项替代"name"选项。
# find ./test -iname "*.Php" ./test/cool.php ./test/subdir/how.php
将搜索项(名称参数)添加单引号或双引号老是更好的作法。
不这样作看起来有时候会起做用,其余时候又会给出奇怪的结果。
默认状况下,find命令递归的遍历整个目录树。很是消耗时间和资源。然而,能够指定目录遍历的深度。例如,咱们不想遍历2,3级子目录,这可使用maxdepth选项完成。
]# find ./test -maxdepth 2 -name '*.php' ./test/cool.php ./test/subdir/how.php # find ./test -maxdepth 1 -name '*.php' ./test/cool.php
第二个示例使用maxdepth 1选项,它意味着不会搜索超过1级深度,仅在当前目录中。
当咱们但愿限制搜索仅在当前目录或最大1级深度子目录,这颇有用,没必要搜索整个目录,而这将须要大量时间 。
就像maxdepth同样,有一个名为mindepth的选项能够实现如其名称所暗示的功能,它将至少搜索n级深度。
也能够搜索与指定名称或模式不匹配的文件,当咱们知道要从搜索中排除哪些文件时,这颇有用。
# find ./test -not -name '*.php' ./test ./test/abc.txt ./test/subdir
因此在上面的例子中咱们发现全部没有php扩展名的文件,都是非php文件。find命令也支持用感叹号替代not
# find ./test ! -name '*.php' ./test ./test/abc.txt ./test/subdir
当指定名称或反转时可使用多个条件。例如:
# find ./test -name 'abc*' ! -name '*.php' ./test/abc.txt
上面的find命令查找文件名以abc开头同时扩展名不是php的文件。这是一个使用find命令构建强大的搜索表达式的示例
OR操做符
当使用多个名称条件时,find命令将用and操做符组合他们,这意味着只有当文件符合全部条件时才被匹配。然而,若是咱们须要执行一个基于or的匹配,find也有'o'开关。
# find ./test -name '*.php' -o -name '*.txt' ./test/abc.txt ./test/cool.php ./test/subdir/how.php
上面的命令搜索以php或txt为扩展名结尾的文件
有时候,咱们但愿仅查找指定名称的文件或目录。find可以轻松的实现。
# find ./test -name 'abc*' ./test/abc.txt ./test/abc Only files # find ./test -type f -name 'abc*' ./test/abc.txt Only directories # find ./test -type d -name 'abc*' ./test/abc
至关有用和方便
所以,假如你但愿在2个单独的目录中进行搜索,一样,命令很是简单
# find ./test ./dir2 -type f -name 'abc*' ./test/abc.txt ./dir2/abcdefg.txt
检查它是否列出了2个独立目录中的文件
Linux中隐藏文件以点号开头(.), 所以,它很容易在名称条件中指定和显示全部隐藏文件。
# find ~ -type f -name '.*'
find命令可使用"perm"来查找指定权限的文件。下列的命令查找权限为0644的文件。
# find . -type f -perm 0644 ./abc.txt ./cool.php ./subdir/how.php ./abc.php
这对查找赋予了错误权限的文件颇有用,这些文件可能致使安全问题。取反也可用于权限检查。
# find . -type f ! -perm 0777 ./abc.txt ./cool.php ./subdir/how.php ./abc.php
find命令的"perm"选项接收与chmod命令同样的字符模式。下列的命令查找全部权限为644以及设置了sgid位的文件。
# find / -perm 2644
相似地,使用1644查找权限为644并设置了粘滞位的文件,"perm"也支持使用替代语法而不是八进制数。
# find / -maxdepth 3 -perm /u=s 2>/dev/null /usr/bin/fusermount /usr/bin/chfn /usr/bin/chsh /usr/bin/chage /usr/bin/gpasswd /usr/bin/newgrp /usr/bin/staprun /usr/bin/mount /usr/bin/crontab /usr/bin/su /usr/bin/umount /usr/bin/pkexec /usr/bin/sudo /usr/bin/passwd /usr/sbin/pam_timestamp_check /usr/sbin/unix_chkpwd /usr/sbin/usernetctl
注意,“2>/dev/null"删除了带有"权限错误”的条目。
查找全部只读文件
oot 66176 Aug 4 2017 /bin/ping [root@lanquark test]# find /etc -maxdepth 1 -perm /u=r /etc /etc/fstab /etc/crypttab /etc/mtab /etc/resolv.conf ...省略后续输出
下面的命令将查找可执行文件
[root@lanquark test]# find /usr/bin -maxdepth 2 -perm /a=x /usr/bin/cp /usr/bin/lua ...省略后续输出
在全部者为root的/root目录下,查找全部或单个名称为tecmint.txt的文件
# find . -user root . ./abc.txt ./cool.php ./subdir ./subdir/how.php ./abc ./abc.php
咱们也能够指定文件的名称或者任何与用户条件相关的名称条件。
# find . -user root -name '*.php' ./cool.php ./subdir/how.php ./abc.php
显而易见,咱们能够如何构建条件以缩小咱们匹配的搜索范围。、
搜索属于某个特定组的全部文件
# find . -group hjm ./abc.txt
你知道你可使用~符号搜索你的家目录吗?
# find ~ -name abc.txt /root/abc.txt /root/test/abc.txt
很简单
搜索文件和目录基于修改日期和时间
另外一个find命令支持的重要搜索条件是修改和访问日期/时间。当咱们想要找出特定时间和日期范围修改的文件时很是方便。让咱们来看一些小例子。
# find / -atime 50
查找修改时间在50之前,100天之内的全部文件
# find / -mtime +50 -mtime -100
查找最近1小时内属性更改的文件。
# find . -cmin -60
查找最近1小时内文件内容更改的全部文件。
# find / -mmin -60
查找最近1小时内访问的全部文件。
# find / -amin -60
基于大小查找文件和目录,查找甩的50M的文件,使用
# find / -size 50M
查找全部大于50M小于100M的文件
# find / -size +50M -size -100M
find命令结合ls和sort命令能够列出最大的文件。
下述命令将显示当前目录和它的子目录下最大的5个文件。这将须要一点时间,取决于命令处理的文件数量。
# find . -type f -exec ls -s {} \; | sort -n | head -5
下列命令使用find命令的"空"选项查找全部空文件。
# find /etc -type f -empty
查找全部的空目录使用类型"d"
# find /etc -type d -empty
真的很是简单和容易
一些高级的操做
find命令不只能够经过一些条件查找文件,它也可使用任何linux命令对这些文件进行操做。例如,咱们想删除一些文件。
下面是一些简单的例子。
假设咱们用find命令找到了文件,现要想要像ls命令那样列出它们,它很简单。
# find . -exec ls -ld {} \; drwxr-xr-x. 4 root root 77 Nov 19 20:28 . -rw-r--r--. 1 root hjm 0 Nov 10 22:55 ./abc.txt -rw-r--r--. 1 root root 0 Nov 10 22:55 ./cool.php drwxr-xr-x. 2 root root 21 Nov 19 20:30 ./subdir -rw-r--r--. 1 root root 0 Nov 19 20:30 ./subdir/how.php drwxr-xr-x. 2 root root 6 Nov 11 00:49 ./abc -rw-r--r--. 1 root root 0 Nov 19 20:28 ./abc.php
下面的命令将删除tmp目录下的全部txt文件。
# find /tmp -type f -name '*.txt' -exec rm -f {} \;
一样的操做也能够在目录上执行,只须要用type d代替type f
让咱们再看一个咱们想要删除大于100M的文件的例子
# find /root -type f -name '*.log' -size +10M -exec rm -f {} \;
总结
这是一个关于linux find命令的快速教程。find命令是linux
终端下最基本的命令,它让搜索文件很是容易,它是系统管理所必须的。所以,学习它。若是有问题,能够在下面的评论区留言。
原文:https://www.binarytides.com/category/linux/linux-commands/