find命令大全,很详细
- 创建测试文件
- 按照权限并指定文件类型搜索
- 搜索权限0660,名称为zheng的文件,然后加上 -ls参数显示
- 权限为0660,名称为zheng的文件 , -type f 指定类型为文件
- 直接指定文件然后按名称搜索
- 模糊查找
- 除了该文件剩下的都显示
- 寻找/test/目录下一天前新增的目录或档案
- 寻找/test/目录下二十四小时更动过的目录或档案
- 寻找/etc/目录下比/etc/passwd日期新的文件或目录
- 寻找/home目录下所有test用户的文件或目录
- 搜索/home下档案属性为f(普通文件)的文档
- 搜索/目录下所有的特殊位(suid,sgid,sbit)并显示详细信息
- 搜索/root目录下所有大于1K的文件
- 查询/root目录下一个小时之前更改的文件,不包括目录
- 查询/root目录下一个小时之后更改的文件,不包括目录
- 三种常见组合命令
- -exec
- 小结
创建测试文件
接下来创建两个不同目录不同权限同名的文件来测试
[root@shell ~ 56]# touch zheng
[root@shell ~ 58]# chmod 660 zheng
[root@shell ~ 59]# cd /
[root@shell / 60]# touch zheng
[root@shell / 61]# chmod 600 zheng
按照权限并指定文件类型搜索
项目 | Value |
---|---|
-perm mode | 文件的权限正好是mode就匹配 |
-perm -mode | 文件的权限包括mode就匹配(该文件还可以拥有额外的权限属性) |
-perm /mode | 文件的权限部分满足mode就匹配 |
网上看到的其他解释,结合看,理解更清晰:
-perm mode :搜尋檔案權限『剛好等於』 mode 的檔案,這個 mode 為類似 chmod
的屬性值,舉例來說, -rwsr-xr-x 的屬性為 4755 !
-perm -mode :搜尋檔案權限『必須要全部囊括 mode 的權限』的檔案,舉例來說,
我們要搜尋 -rwxr–r-- ,亦即 0744 的檔案,使用 -perm -0744,
當一個檔案的權限為 -rwsr-xr-x ,亦即 4755 時,也會被列出來,
因為 -rwsr-xr-x 的屬性已經囊括了 -rwxr–r-- 的屬性了。
-perm /mode :搜尋檔案權限『包含任一 mode 的權限』的檔案,舉例來說,我們搜尋
-rwxr-xr-x ,亦即 -perm +755 時,但一個檔案屬性為 -rw-------
也會被列出來,因為他有 -rw… 的屬性存在!
搜索权限0660,名称为zheng的文件,然后加上 -ls参数显示
搜索权限0660,名称为zheng的文件,然后加上 -ls参数显示
[root@shell / 91]# find / -perm 0660 -name "zheng" -ls
33627914 0 -rw-rw---- 1 root root 0 Aug 4 11:02 ./root/zheng
权限为0660,名称为zheng的文件 , -type f 指定类型为文件
-type f/d -name “—”
参数 | 名称 | — |
---|---|---|
f | file | 文件 |
d | dir | 目录 |
搜索------权限为0660,名称为zheng的文件 , -type f 指定类型为文件 ,然后加上 -ls参数显示
[root@shell / 93]# find / -perm 0660 -type f -name "zheng" -ls
33627914 0 -rw-rw---- 1 root root 0 Aug 4 11:02 ./root/zheng
搜索------权限为0600,名称为zheng的文件 , -type f 指定类型为文件 ,然后加上 -ls参数显示
[root@shell / 94]# find / -perm 0600 -type f -name "zheng" -ls
29603 0 -rw------- 1 root root 0 Aug 4 11:09 ./zheng
搜索-----包括权限0600,名称为zheng的文件 , -type f 指定类型为文件 ,然后加上 -ls参数显示
[root@shell ~ 105]# find / -perm -0600 -type f -name "zheng" -ls
33627914 0 -rw-rw---- 1 root root 0 Aug 4 11:02 /root/zheng
29603 0 -rw------- 1 root root 0 Aug 4 11:09 /zheng
直接指定文件然后按名称搜索
直接指定文件然后按名称搜索
[root@shell ~ 108]# find / -type f -name "zheng" -ls
33627914 0 -rw-rw---- 1 root root 0 Aug 4 11:02 /root/zheng
29603 0 -rw------- 1 root root 0 Aug 4 11:09 /zheng
找出.txt .log .sh这种文件
[root@shell ~ 108]# mkdir test
[root@shell ~ 130]# cd /test
[root@shell test 132]# touch 1.txt
[root@shell test 133]# touch 2.txt
[root@shell test 134]# touch 3.txt
[root@shell test 135]# touch test.txt
[root@shell ~ 140]# find /test -name "*.txt"
/test/1.txt
/test/2.txt
/test/3.txt
/test/test.txt
模糊查找
[root@zheng ~]# find / -type f -name "*firewall*"
除了该文件剩下的都显示
寻找/test/目录下一天前新增的目录或档案
[root@shell ~ 8]# find /test -ctime +1
寻找/test/目录下二十四小时更动过的目录或档案
[root@shell ~ 9]# find /test -mtime 0
/test
/test/11sdas
/test/ceshi
/test/ceshi/111
/test/ceshi/222
/test/1.txt
/test/2.txt
/test/3.txt
/test/test.txt
寻找/etc/目录下比/etc/passwd日期新的文件或目录
[root@shell ~ 5]# find /etc/ -newer /etc/passwd
/etc/
/etc/resolv.conf
/etc/rpm
/etc/yum.repos.d
/etc/yum.repos.d/1
/etc/yum.repos.d/local.repo
/etc/lvm/archive
/etc/lvm/archive/cl_00000-822119387.vg
/etc/lvm/backup
/etc/lvm/backup/cl
/etc/group
/etc/gshadow
/etc/ssh
/etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_rsa_key.pub
/etc/ssh/ssh_host_ecdsa_key
/etc/ssh/ssh_host_ecdsa_key.pub
/etc/ssh/ssh_host_ed25519_key
/etc/ssh/ssh_host_ed25519_key.pub
/etc/motd
/etc/profile
/etc/profile.d
/etc/systemd/system
/etc/systemd/system/default.target
/etc/shadow
/etc/sysconfig
/etc/sysconfig/network-scripts
/etc/sysconfig/network-scripts/ifcfg-ens33
/etc/udev
/etc/udev/hwdb.bin
/etc/ld.so.cache
/etc/rc.d
/etc/cron.daily
/etc/tuned/active_profile
/etc/audit
/etc/audit/audit.rules
/etc/chrony.keys
/etc/hostname
/etc/aliases.db
寻找/home目录下所有test用户的文件或目录
[root@shell ~ 13]# find /home -user test
/home/test
/home/test/.bash_logout
/home/test/.bash_profile
/home/test/.bashrc
搜索/home下档案属性为f(普通文件)的文档
[root@shell ~ 14]# find /home -type f
/home/test/.bash_logout
/home/test/.bash_profile
/home/test/.bashrc
搜索/目录下所有的特殊位(suid,sgid,sbit)并显示详细信息
[root@shell ~ 17]# find / -perm /7000 -ls
7905 0 drwxrwxrwt 2 root root 40 Aug 1 08:27 /dev/mqueue
8115 0 drwxrwxrwt 2 root root 40 Aug 1 08:27 /dev/shm
find: ‘/proc/7975/task/7975/fd/6’: No such file or directory
find: ‘/proc/7975/task/7975/fdinfo/6’: No such file or directory
find: ‘/proc/7975/fd/6’: No such file or directory
find: ‘/proc/7975/fdinfo/6’: No such file or directory
8669 0 drwxr-sr-x 3 root systemd-journal 60 Aug 1 08:27 /run/log/journal
8670 0 drwxr-s--- 2 root systemd-journal 60 Aug 1 08:27 /run/log/journal/1db242910d784e618ae215be56fca45e
69 0 drwxrwxrwt 3 root root 86 Aug 4 05:50 /var/tmp
50750322 0 drwxrwxrwt 2 root root 6 Aug 1 08:27 /var/tmp/systemd-private-08f30a55c1524cc9b6e1e46aed930978-vmtoolsd.service-UzS6PH/tmp
16777288 0 drwxrwxrwt 8 root root 222 Aug 4 11:56 /tmp
16912704 0 drwxrwxrwt 2 root root 6 Jul 22 15:17 /tmp/.X11-unix
428909 0 drwxrwxrwt 2 root root 6 Jul 22 15:17 /tmp/.font-unix
16912705 0 drwxrwxrwt 2 root root 6 Jul 22 15:17 /tmp/.Test-unix
33849296 0 drwxrwxrwt 2 root root 6 Jul 22 15:17 /tmp/.ICE-unix
50548164 0 drwxrwxrwt 2 root root 6 Jul 22 15:17 /tmp/.XIM-unix
16777824 0 drwxrwxrwt 3 root root 25 Aug 1 08:27 /tmp/systemd-private-08f30a55c1524cc9b6e1e46aed930978-vmtoolsd.service-2HO79q/tmp
50416139 16 -r-xr-sr-x 1 root tty 15344 Jun 9 2014 /usr/bin/wall
50421527 32 -rwsr-xr-x 1 root root 32008 Nov 5 2016 /usr/bin/fusermount
50533234 24 -rws--x--x 1 root root 23960 Nov 5 2016 /usr/bin/chfn
50533127 64 -rwsr-xr-x 1 root root 64240 Nov 5 2016 /usr/bin/chage
50533128 80 -rwsr-xr-x 1 root root 78216 Nov 5 2016 /usr/bin/gpasswd
50533130 44 -rwsr-xr-x 1 root root 41776 Nov 5 2016 /usr/bin/newgrp
50533236 24 -rws--x--x 1 root root 23872 Nov 5 2016 /usr/bin/chsh
50533315 32 -rwsr-xr-x 1 root root 32088 Nov 5 2016 /usr/bin/su
50533268 44 -rwsr-xr-x 1 root root 44232 Nov 5 2016 /usr/bin/mount
50533319 32 -rwsr-xr-x 1 root root 31968 Nov 5 2016 /usr/bin/umount
50533325 20 -rwxr-sr-x 1 root tty 19536 Nov 5 2016 /usr/bin/write
50723377 128 ---s--x--x 1 root root 130776 Nov 5 2016 /usr/bin/sudo
50548188 28 -rwsr-xr-x 1 root root 27680 Nov 5 2016 /usr/bin/pkexec
50563148 60 -rwsr-xr-x 1 root root 57552 Mar 31 2016 /usr/bin/crontab
50796663 40 -rwx--s--x 1 root slocate 40512 Nov 5 2016 /usr/bin/locate
50721240 300 ---x--s--x 1 root nobody 306360 Nov 11 2016 /usr/bin/ssh-agent
50723370 28 -rwsr-xr-x 1 root root 27832 Jun 10 2014 /usr/bin/passwd
289972 12 -rwsr-xr-x 1 root root 11224 Nov 5 2016 /usr/sbin/pam_timestamp_check
289974 36 -rwsr-xr-x 1 root root 36280 Nov 5 2016 /usr/sbin/unix_chkpwd
435284 12 -rwxr-sr-x 1 root root 11224 Nov 5 2016 /usr/sbin/netreport
435289 12 -rwsr-xr-x 1 root root 11296 Nov 5 2016 /usr/sbin/usernetctl
620454 216 -rwxr-sr-x 1 root postdrop 218552 Jun 9 2014 /usr/sbin/postdrop
610719 256 -rwxr-sr-x 1 root postdrop 259992 Jun 9 2014 /usr/sbin/postqueue
50548191 16 -rwsr-xr-x 1 root root 15432 Nov 5 2016 /usr/lib/polkit-1/polkit-agent-helper-1
16912710 312 -rwsr-x--- 1 root dbus 318424 Nov 6 2016 /usr/lib64/dbus-1/dbus-daemon-launch-helper
16895470 12 -rwx--s--x 1 root utmp 11192 Jun 9 2014 /usr/libexec/utempter/utempter
16899139 452 ---x--s--x 1 root ssh_keys 461496 Nov 11 2016 /usr/libexec/openssh/ssh-keysign
搜索/root目录下所有大于1K的文件
-size n[cwbkMG]
File uses n units of space. The following suffixes can be used:
`b' for 512-byte blocks (this is the default if no suffix is
used)
`c' for bytes
`w' for two-byte words
`k' for Kilobytes (units of 1024 bytes)
`M' for Megabytes (units of 1048576 bytes)
`G' for Gigabytes (units of 1073741824 bytes)
[root@shell ~ 32]# find /root -size +1b
/root/anaconda-ks.cfg
/root/.bash_history
/root/.viminfo
/root/2.txt
查询/root目录下一个小时之前更改的文件,不包括目录
[root@shell ~ 35]# find /root/ -mmin +60 ! -type d -ls
33986221 4 -rw-r--r-- 1 root root 18 Dec 28 2013 /root/.bash_logout
33986222 4 -rw-r--r-- 1 root root 176 Dec 28 2013 /root/.bash_profile
33986224 4 -rw-r--r-- 1 root root 100 Dec 28 2013 /root/.cshrc
33986225 4 -rw-r--r-- 1 root root 129 Dec 28 2013 /root/.tcshrc
33574979 4 -rw------- 1 root root 1264 Jul 22 15:19 /root/anaconda-ks.cfg
33575002 8 -rw------- 1 root root 7519 Aug 4 07:54 /root/.bash_history
33615822 4 -rw-r--r-- 1 root root 176 Aug 3 09:12 /root/.bashrc
34001268 4 -rw------- 1 root root 625 Aug 4 00:00 /root/.viminfo
33575005 4 -rw-r--r-- 1 root root 1031 Aug 4 11:56 /root/2.txt
33574983 0 lrwxrwxrwx 1 root root 5 Aug 4 00:49 /root/3.txt -> 1.txt
33627914 0 -rw-rw---- 1 root root 0 Aug 4 11:02 /root/zheng
33627916 0 -rw-r--r-- 1 root root 0 Aug 4 12:52 /root/zheng.txt
33627917 0 -rw-r--r-- 1 root root 0 Aug 4 12:52 /root/zheng.c
33627918 0 -rw-r--r-- 1 root root 0 Aug 4 12:52 /root/zheng.sh
50796674 0 -rw-r--r-- 1 root root 0 Aug 4 12:58 /root/test/1.txt
50796675 0 -rw-r--r-- 1 root root 0 Aug 4 12:58 /root/test/2.txt
50796676 0 -rw-r--r-- 1 root root 0 Aug 4 12:58 /root/test/3.txt
50796677 0 -rw-r--r-- 1 root root 0 Aug 4 12:58 /root/test/test.txt
查询/root目录下一个小时之后更改的文件,不包括目录
[root@shell ~ 36]# find /root/ -mmin -60 ! -type d -ls
33627920 4 -rw------- 1 root root 41 Aug 4 21:54 /root/.lesshst
三种常见组合命令
|xargs 管道命令
衔接管道
xargs后面添加命令
[root@zheng ~]# find /zyf/logs/ -type f -name "*.log" |xargs ls -l
-rw-r--r--. 1 root root 1340586 Feb 1 2020 /zyf/logs/access_www_2020-02-01.log
-rw-r--r--. 1 root root 1340586 Feb 2 2020 /zyf/logs/access_www_2020-02-02.log
-rw-r--r--. 1 root root 1340586 Feb 3 2020 /zyf/logs/access_www_2020-02-03.log
-rw-r--r--. 1 root root 1340586 Feb 4 2020 /zyf/logs/access_www_2020-02-04.log
-rw-r--r--. 1 root root 1340586 Feb 5 2020 /zyf/logs/access_www_2020-02-05.log
-rw-r--r--. 1 root root 1340586 Feb 6 2020 /zyf/logs/access_www_2020-02-06.log
-rw-r--r--. 1 root root 1340586 Feb 7 2020 /zyf/logs/access_www_2020-02-07.log
-rw-r--r--. 1 root root 1340586 Feb 8 2020 /zyf/logs/access_www_2020-02-08.log
-rw-r--r--. 1 root root 1340586 Feb 9 2020 /zyf/logs/access_www_2020-02-09.log
-rw-r--r--. 1 root root 1340586 Feb 10 2020 /zyf/logs/access_www_2020-02-10.log
命令 + $()
把$()里的内容传递到前面的命令
组合命令
[root@zheng ~]# ls -l $(find /zyf/logs/ -type f -name "*.log")
-rw-r--r--. 1 root root 1340586 Feb 1 2020 /zyf/logs/access_www_2020-02-01.log
-rw-r--r--. 1 root root 1340586 Feb 2 2020 /zyf/logs/access_www_2020-02-02.log
-rw-r--r--. 1 root root 1340586 Feb 3 2020 /zyf/logs/access_www_2020-02-03.log
-rw-r--r--. 1 root root 1340586 Feb 4 2020 /zyf/logs/access_www_2020-02-04.log
-rw-r--r--. 1 root root 1340586 Feb 5 2020 /zyf/logs/access_www_2020-02-05.log
-rw-r--r--. 1 root root 1340586 Feb 6 2020 /zyf/logs/access_www_2020-02-06.log
-rw-r--r--. 1 root root 1340586 Feb 7 2020 /zyf/logs/access_www_2020-02-07.log
-rw-r--r--. 1 root root 1340586 Feb 8 2020 /zyf/logs/access_www_2020-02-08.log
-rw-r--r--. 1 root root 1340586 Feb 9 2020 /zyf/logs/access_www_2020-02-09.log
-rw-r--r--. 1 root root 1340586 Feb 10 2020 /zyf/logs/access_www_2020-02-10.log
-exec
-exec 是执行的意思
{ }花括号是前面命令执行的结果
\ ;组合用法,撬棍加;不要撬棍 \ 要不然会有别的意思 ,报错
[root@zheng ~]# find /zyf/logs/ -type f -name "*.log" -exec ls -l {} \;
-rw-r--r--. 1 root root 1340586 Feb 1 2020 /zyf/logs/access_www_2020-02-01.log
-rw-r--r--. 1 root root 1340586 Feb 2 2020 /zyf/logs/access_www_2020-02-02.log
-rw-r--r--. 1 root root 1340586 Feb 3 2020 /zyf/logs/access_www_2020-02-03.log
-rw-r--r--. 1 root root 1340586 Feb 4 2020 /zyf/logs/access_www_2020-02-04.log
-rw-r--r--. 1 root root 1340586 Feb 5 2020 /zyf/logs/access_www_2020-02-05.log
-rw-r--r--. 1 root root 1340586 Feb 6 2020 /zyf/logs/access_www_2020-02-06.log
-rw-r--r--. 1 root root 1340586 Feb 7 2020 /zyf/logs/access_www_2020-02-07.log
-rw-r--r--. 1 root root 1340586 Feb 8 2020 /zyf/logs/access_www_2020-02-08.log
-rw-r--r--. 1 root root 1340586 Feb 9 2020 /zyf/logs/access_www_2020-02-09.log
-rw-r--r--. 1 root root 1340586 Feb 10 2020 /zyf/logs/access_www_2020-02-10.log