find命令详解

1.name: 指定文件名

例子1. 找到以xen结尾的文件
[root@xen ~]# find / -name “*xen”
/sys/firmware/dmi/entries/15-0/system_event_log/change_toxen
/etc/yum.repos.d/xen
/var/spool/mail/xen
/home/xen

例子2:找到包含xen的文件
[root@xen ~]# find / -name “xen” | head -3
/sys/bus/hid/drivers/xensington
/sys/devices/pci0000:00/0000:00:00.0/broxen_parity_status
/sys/devices/pci0000:00/0000:00:01.0/broxen_parity_status

2. perm:指定文件权限

例子1:找到所有644权限的文件
[root@xen ~]# find / -perm 644 | head -3
/boot/grub2/device.map
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod
[root@xen ~]# ls -l /boot/grub2/device.map
-rw-r–r–. 1 root root 64 Feb 26 22:25 /boot/grub2/device.map

例子2:模糊匹配644权限
[root@xen ~]# find / -perm -644 | head -3
/boot/efi
/boot/efi/EFI
/boot/efi/EFI/centos
[root@xen ~]# ls -l /boot/efi
total 0
drwxr-xr-x. 3 root root 20 Feb 26 22:21 EFI

3. user:指定属主

例子1:找到属主是xen的文件
[root@xen ~]# find /etc -user xen
[root@xen ~]# find / -user xen
find: ‘/proc/1672/task/1672/fd/6’: No such file or directory
find: ‘/proc/1672/task/1672/fdinfo/6’: No such file or directory
find: ‘/proc/1672/fd/5’: No such file or directory
find: ‘/proc/1672/fdinfo/5’: No such file or directory
/var/spool/mail/xen
[root@xen ~]# ls -l /var/spool/mail/xen
-rw-rw—- 1 xen mail 0 Mar 11 18:16 /var/spool/mail/xen

4.group:指定属组

例子1:
[root@xen ~]# ls -l /var/spool/mail/xen
-rw-rw—- 1 xen mail 0 Mar 11 18:16 /var/spool/mail/xen
[root@xen ~]# find / -group xen
find: ‘/proc/1709/task/1709/fd/6’: No such file or directory
find: ‘/proc/1709/task/1709/fdinfo/6’: No such file or directory
find: ‘/proc/1709/fd/5’: No such file or directory
find: ‘/proc/1709/fdinfo/5’: No such file or directory
/home/xen
/home/xen/.bash_logout
/home/xen/.bash_profile
/home/xen/.bashrc
/home/xen/.bash_history
[root@xen ~]# ls -l /home/xen -d
drwx—— 2 xen xen 83 Mar 11 18:16 /home/xen

5. nouser:指定无属主

例子1:
[root@xen ~]# useradd xenxen
[root@xen ~]# touch test
[root@xen ~]# chown xenxen: test
[root@xen ~]# userdel xenxen
[root@xen ~]# find / -nouser
find: ‘/proc/1811/task/1811/fd/6’: No such file or directory
find: ‘/proc/1811/task/1811/fdinfo/6’: No such file or directory
find: ‘/proc/1811/fd/5’: No such file or directory
find: ‘/proc/1811/fdinfo/5’: No such file or directory
/root/test
/var/spool/mail/xenxen
/home/xenxen
/home/xenxen/.bash_logout
/home/xenxen/.bash_profile

6. nogroup:指定无属主

[root@xen ~]# find / -nogroup
find: ‘/proc/1848/task/1848/fd/6’: No such file or directory
find: ‘/proc/1848/task/1848/fdinfo/6’: No such file or directory
find: ‘/proc/1848/fd/5’: No such file or directory
find: ‘/proc/1848/fdinfo/5’: No such file or directory
/root/test
/home/xenxen
/home/xenxen/.bash_logout
/home/xenxen/.bash_profile
/home/xenxen/.bashrc

7.type:指定文件类型

例子1:找到所有的链接文件
[root@xen ~]# find / -type l | head -3
/dev/cdrom
/dev/snd/by-path/pci-0000:02:02.0
/dev/initctl
[root@xen ~]# ls -l /dev/cdrom
lrwxrwxrwx 1 root root 3 May 24 19:19 /dev/cdrom -> sr0
[root@xen ~]#

例子2: 找到所有普通文件
[root@xen ~]# find / -type f | head -3
/boot/grub2/device.map
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod
[root@xen ~]# ls -l /boot/grub2/device.map
-rw-r–r–. 1 root root 64 Feb 26 22:25 /boot/grub2/device.map

8. size:指定文件大小

例子1:找到大于100M的文件
[root@xen ~]# find / -size +100M
/proc/kcore
find: ‘/proc/2018/task/2018/fd/6’: No such file or directory
find: ‘/proc/2018/task/2018/fdinfo/6’: No such file or directory
find: ‘/proc/2018/fd/5’: No such file or directory
find: ‘/proc/2018/fdinfo/5’: No such file or directory
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/root/gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm
^C
[root@xen ~]# ls -lh /root/gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm
-rw-r–r– 1 root root 265M Nov 13 2018 /root/gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm

9. mtime: 指文件修改时间,权限,属主,属组

例子1:找到文件修改时间一天以上的
[root@xen ~]# find / -mtime +1 | head -3
/boot
/boot/efi
/boot/efi/EFI
[root@xen ~]# stat /boot
File: ‘/boot’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 801h/2049d Inode: 64 Links: 5
Access: (0555/dr-xr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-05-24 19:31:37.453000000 +0800
Modify: 2019-02-26 22:25:57.574000000 +0800
Change: 2019-03-14 18:49:48.066000000 +0800
Birth: –

10. atime:指定文件访问时间

[root@xen ~]# find / -atime +1 | head -3
/boot/grub2/device.map
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod
^C
[root@xen ~]# stat /boot/grub2/device.map
File: ‘/boot/grub2/device.map’
Size: 64 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 1572929 Links: 1
Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-02-26 22:25:05.730000000 +0800
Modify: 2019-02-26 22:25:05.582000000 +0800
Change: 2019-02-26 22:25:05.582000000 +0800
Birth: –

11.ctime:指定文件内容

例子1:指定文件内容修改一天以上
[root@xen ~]# find / -ctime +1 | head -3
/boot
/boot/efi
/boot/efi/EFI
C
[root@xen ~]# stat /boot
File: ‘/boot’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 801h/2049d Inode: 64 Links: 5
Access: (0555/dr-xr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-05-24 19:31:37.453000000 +0800
Modify: 2019-02-26 22:25:57.574000000 +0800
Change: 2019-03-14 18:49:48.066000000 +0800
Birth: –

12. exec: 执行命令

例子1:删除无属主的文件
[root@xen ~]# find / -nouser
find: ‘/proc/2395/task/2395/fd/6’: No such file or directory
find: ‘/proc/2395/task/2395/fdinfo/6’: No such file or directory
find: ‘/proc/2395/fd/5’: No such file or directory
find: ‘/proc/2395/fdinfo/5’: No such file or directory
/root/test
/var/spool/mail/xenxen
/home/xenxen
/home/xenxen/.bash_logout
/home/xenxen/.bash_profile
/home/xenxen/.bashrc
[root@xen ~]# find / -nouser -exec rm -rf {} ;
find: missing argument to `-exec’
[root@xen ~]# find / -nouser -exec rm -rf {} ;
find: ‘/proc/2451/task/2451/fd/6’: No such file or directory
find: ‘/proc/2451/task/2451/fdinfo/6’: No such file or directory
find: ‘/proc/2451/fd/5’: No such file or directory
find: ‘/proc/2451/fdinfo/5’: No such file or directory
find: ‘/home/xenxen’: No such file or directory
[root@xen ~]# find / -nouser
find: ‘/proc/2463/task/2463/fd/6’: No such file or directory
find: ‘/proc/2463/task/2463/fdinfo/6’: No such file or directory
find: ‘/proc/2463/fd/5’: No such file or directory
find: ‘/proc/2463/fdinfo/5’: No such file or directory

例子2:xargs
[root@xen ~]# touch {1..100}.txt
[root@xen ~]# powd
-bash: powd: command not found
[root@xen ~]# pwd
/root
[root@xen ~]# ls
100.txt 19.txt 28.txt 37.txt 46.txt 55.txt 64.txt 73.txt 82.txt 91.txt anaconda-ks.cfg
10.txt 1.txt 29.txt 38.txt 47.txt 56.txt 65.txt 74.txt 83.txt 92.txt a.out
11.txt 20.txt 2.txt 39.txt 48.txt 57.txt 66.txt 75.txt 84.txt 93.txt gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm
12.txt 21.txt 30.txt 3.txt 49.txt 58.txt 67.txt 76.txt 85.txt 94.txt xen1
13.txt 22.txt 31.txt 40.txt 4.txt 59.txt 68.txt 77.txt 86.txt 95.txt
14.txt 23.txt 32.txt 41.txt 50.txt 5.txt 69.txt 78.txt 87.txt 96.txt
15.txt 24.txt 33.txt 42.txt 51.txt 60.txt 6.txt 79.txt 88.txt 97.txt
16.txt 25.txt 34.txt 43.txt 52.txt 61.txt 70.txt 7.txt 89.txt 98.txt
17.txt 26.txt 35.txt 44.txt 53.txt 62.txt 71.txt 80.txt 8.txt 99.txt
18.txt 27.txt 36.txt 45.txt 54.txt 63.txt 72.txt 81.txt 90.txt 9.txt
[root@xen ~]# find /root -name “*txt” | xargs rm -rf
[root@xen ~]ls
anaconda-ks.cfg a.out gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm xen1

13.find可以使用-a和-o或!

例子1:找到所有事普通文件并且权限是777
[root@xen ~]# find / -type f -a -perm 644 | head -3

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容