天天看点

Linux find找你想要的

find命令概述

Linux下find命令在目录结构中搜索文件,并执行指定的操作。Linux下find命令提供了相当多的查找条件,功能很强大。由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限。 在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。

1.命令格式:

find pathname -options [-print -exec -ok ...]

2.命令功能:

用于在文件树种查找文件,并作出相应的处理 

3.命令参数:

pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。 

-print:find命令将匹配的文件输出到标准输出。 

-exec:find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' {  } \;,注意{   }和\;之间的空格。 

-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。

4.命令选项:

-name   按照文件名查找文件。

-perm   按照文件权限来查找文件。

-prune  使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。

-user   按照文件属主来查找文件。

-group  按照文件所属的组来查找文件。

-mtime -n +n  按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。

-nogroup  查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。

-nouser   查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。

-newer file1 ! file2  查找更改时间比文件file1新但比文件file2旧的文件。

-type  查找某一类型的文件,诸如:

b - 块设备文件。

d - 目录。

c - 字符设备文件。

p - 管道文件。

l - 符号链接文件。

f - 普通文件。

-size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。

-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。

-fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。

-mount:在查找文件时不跨越文件系统mount点。

-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。

-cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。

另外,下面三个的区别:

-amin n   查找系统中最后N分钟访问的文件

-atime n  查找系统中最后n*24小时访问的文件

-cmin n   查找系统中最后N分钟被改变文件状态的文件

-ctime n  查找系统中最后n*24小时被改变文件状态的文件

-mmin n   查找系统中最后N分钟被改变文件数据的文件

-mtime n  查找系统中最后n*24小时被改变文件数据的文件

下面提供了根据这些要求查找文件的示例。在某些命令中,错误(例如试图列出你没有读取权限的文件)输出将被发送到 ​

​/dev/null​

​,以便我们不必查看它。或者,我们可以简单地以 root 身份运行以避免这个问题。

find命令循序渐进

选择起点

使用 ​​find​​​,你可以选择一个起点或从你所在的位置开始。要选择的搜索的起点,请在单词 ​​find​​​ 后输入它。例如,​​find /usr​​​ 或 ​​find ./bin​​​ 将在 ​

​/usr​

​​ 目录或当前位置下的 ​

​bin​

​​ 目录开始搜索,而 ​​find ~​​ 将在你的主目录中开始搜索,即使你当前位于当前文件系统中的其他位置。

  • 第一部分:查找名称查找文件的基本查找命令
  • 第二部分:根据他们的权限查找文件
  • 第三部分:基于所有者和组的搜索文件
  • 第四部分:根据日期和时间查找文件和目录
  • 第五部分:根据大小查找文件和目录

第一部分:查找名称查找文件的基本查找命令

最常用的搜索策略之一是按名称搜索文件。这需要使用 ​

​-name​

​ 选项。

默认情况下,​​find​​​ 会显示找到的文件的完整路径。如果你在命令中添加 ​

​-print​

​,你会看到同样的结果。如果你想查看与文件相关的详细信息,例如:文件的长度、权限等,你需要在你的 ​​find​​​命令的末尾添加 ​

​-ls​

​ 参数。

[root@www ~]# find ~/ -name test.sh 
/root/test.sh
[root@www ~]# find ~/ -name test.sh -print
/root/test.sh
[root@www ~]# find ~/ -name test.sh -print -ls
/root/test.sh
33619982    4 -rwxrwxrwx   1 root     root          332 May  8 09:40 /root/test.sh



#这里由两个-name,第一个是先找到.rpm包,第二个是找到以zip开头的包
[root@www ~]# find / -name "*.rpm"  -name "zip*"  
/mnt/dvd/Packages/zip-3.0-11.el7.x86_64.rpm      

通过索引节点号查找文件,你可以通过用于维护文件元数据(即除文件内容和文件名之外的所有内容)的索引节点来查找文件。

[root@www ~]# find . -inum 33604465 -ls 2>/dev/null 
33604465    4 -rwxrwxrwx   1 root     root         1323 May  8 16:57 ./auto_change_ip.sh      

第二部分:根据他们的权限查找文件

查找权限为664的文件

[root@www ~]# find / -perm 644 -ls | head -n 3 
786497    4 -rw-r--r--   1 root     root           84 Feb 24 21:58 /boot/grub2/device.map
262210    8 -rw-r--r--   1 root     root         8068 Feb 24 21:58 /boot/grub2/i386-pc/gcry_rmd160.mod
262211   12 -rw-r--r--   1 root     root         9932 Feb 24 21:58 /boot/grub2/i386-pc/acpi.mod      

查找根目录下所有权限不是777的文件

[root@www ~]# find / -type f ! -perm 777 | head -n 3 
/boot/grub2/device.map
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod      

找到SUID文件 

[root@www ~]# find / -perm /u=s 2>/dev/null -ls | head -n 3
50410615   32 -rwsr-xr-x   1 root     root        32008 Aug  4  2017 /usr/bin/fusermount
50671900   24 -rws--x--x   1 root     root        23960 Aug  4  2017 /usr/bin/chfn
50671902   24 -rws--x--x   1 root     root        23872 Aug  4  2017 /usr/bin/chsh

[root@www ~]# find / -perm /g=s -ls  2>/dev/null | head -n 3
  8642    0 drwxr-sr-x   3 root     systemd-journal       60 Mar  5 20:15 /run/log/journal
  8643    0 drwxr-s---   2 root     systemd-journal       80 May 18 14:29 /run/log/journal/2a49177f4460454c986b78b936e2d28b
50407473   16 -r-xr-sr-x   1 root     tty         15344 Jun 10  2014 /usr/bin/wall
      

第三部分:基于所有者和组的搜索文件

在下面这个命令中,我们寻找一个被称为 nginx 的多用户组拥有的文件。 

[root@www ~]# find / -group nginx -ls 2>null | head -2
366823    0 dr-xr-xr-x   9 nginx    nginx           0 May 12 11:26 /proc/90890
388764    0 dr-xr-xr-x   3 nginx    nginx           0 May 12 11:26 /proc/90890/task      

查找没有所有者或组的文件,你可以使用如下命令所示的 ​

​-nouser​

​ 选项来查找不属于当前系统上的任何用户的文件。

# find /tmp -nouser -ls
262204 4 -rwx------ 1 1016 1016 17 Feb 17 16:42 /tmp/hello      

请注意,该列表显示了旧用户的 UID 和 GID,这清楚地表明该用户未在系统上定义。这种命令将查找帐户已从系统中删除的用户创建在主目录之外的文件,或者在用户帐户被删除后而未被删除的主目录中创建的文件。类似地,​

​-nogroup​

​ 选项也会找到这样的文件,尤其是当这些用户是相关组的唯一成员时。

查找具有特定文件所有者或组的文件,按所有者或组查找文件也非常简单。

[root@www ~]# find / -user root -name "*.png" -ls | head -2
17050148    0 lrwxrwxrwx   1 root     root           56 Feb 24 21:52 /etc/favicon.png -> /usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png
   110   20 -rw-r--r--   1 root     root        16443 Dec 17  2013 /usr/share/doc/grub2-common-2.02/font_char_metrics.png      

第四部分:根据日期和时间查找文件和目录

按上次更新时间查找文件

在此命令中,我们在特定用户的主目录中查找过去 24 小时内更新过的文件。

[root@www ~]# find / -mtime -1 -ls | head -2
  9138    0 drwxr-xr-x   2 root     root         3060 May 12 09:59 /dev/char
358758    0 lrwxrwxrwx   1 root     root           18 May 12 09:59 /dev/char/189:149 -> ../bus/usb/002/022      

按上次更改权限的时间查找文件

​-ctime​

​ 选项可以帮助你查找在某个参考时间范围内状态(如权限)发生更改的文件。以下是查找在最后一天内权限发生更改的文件的示例:

[root@www ~]# find / -ctime -1  -ls 2>/dev/null | head -2
  9138    0 drwxr-xr-x   2 root     root         3060 May 12 09:59 /dev/char
358758    0 lrwxrwxrwx   1 root     root           18 May 12 09:59 /dev/char/189:149 -> ../bus/usb/002/022      

按上次访问的时间查找文件

在这个命令中,我们使用 ​

​-atime​

​ 选项查找在过去两天内访问过的本地 png文件。

[root@www ~]# find / -name "*.png" -atime -2
/etc/favicon.png
/usr/share/pixmaps/fedora-gdm-logo.png
/usr/share/kde4/apps/kdm/themes/CentOS7/system-logo-white.png
/usr/share/kde4/apps/ksplash/Themes/CentOS7/2560x1600/logo.png      

根据文件相对于另一个文件的时间来查找文件

你可以使用 ​

​-newer​

​ 选项来查找比其他文件更新的文件。

[root@www ~]# find . -newer test -ls
33579105    4 dr-xr-x---   6 root     root         4096 May 12 12:10 .
33621474   28 -rw-------   1 root     root        24731 May 12 12:10 ./.bash_history
33594625    4 -rw-------   1 root     root           79 Mar  5 17:25 ./.local/share/lftp/cwd_history      

没有相应的 ​

​-older​

​​ 选项,但是你可以用 ​

​! -newer​

​ (即更旧)得到类似的结果,它们基本上一样。

第五部分:根据大小查找文件和目录

大多数情况下,Linux 用户会搜索比选定大小要大的文件。例如,要查找大于 1 千兆字节的文件,你可以使用这样的命令,其中 ​

​+1G​

​ 表示“大于 1 千兆字节”:

[root@www ~]# find / -size +1G -ls  2>/dev/null 
4026532033    0 -r--------   1 root     root     140737486266368 May 12 11:32 /proc/kcore
33579125 4415488 -rw-r--r--   1 root     root     4521459712 Aug 27  2019 /root/CentOS-7.4-x86_64-DVD-1708.iso      

按类型查找文件

通过文件类型找到一个文件,你有很多选项——常规文件、目录、块和字符文件等等。以下是文件类型选项列表: 

b      块特殊文件(缓冲的)
c      字符特殊文件(无缓冲的)
d      目录
p      命名管道(FIFO)
f      常规文件
l      符号链接
s      套接字      

这里有一个寻找符号链接的例子:

[root@www ~]# find / -type l  -ls | head -2
 14762    0 lrwxrwxrwx   1 root     root            3 Mar  5 20:15 /dev/cdrom -> sr0
 15856    0 lrwxrwxrwx   1 root     root           12 Mar  5 20:15 /dev/snd/by-path/pci-0000:02:02.0 -> ../controlC0      
[root@www ~]# find / -maxdepth 3 -name "*loop"
/sys/module/loop