linux系统find命令详解+xargs命令 、exec命令
find
作用:查找文件
1.name: 指定文件名
例子1. 找到以du结尾的文件
╭─root@localhost.localdomain ~
╰─➤ find / -name "*du" | head -3
/var/lib/AccountsService/users/du
/var/db/sudo/du
/var/spool/mail/du
例子2:找到包含du的文件
╭─root@localhost.localdomain ~
╰─➤ find / -name "*du*" | head -3
./.oh-my-zsh/themes/duellj.zsh-theme
./.zcompdump-localhost-5.0.2
./apr-1.5.2/file_io/os2/filedup.c
2. perm:指定文件权限
例子1:找到所有644权限的文件
╭─root@localhost.localdomain ~
╰─➤ find / -perm 644 | head -3
/boot/grub/splash.xpm.gz
/boot/.vmlinuz-3.10.0-514.el7.x86_64.hmac
/boot/config-3.10.0-514.el7.x86_64
╭─root@localhost.localdomain ~
╰─➤ ll /boot/grub/splash.xpm.gz
-rw-r--r--. 1 root root 1.4K 11月 16 2011 /boot/grub/splash.xpm.gz
例子2:模糊匹配644权限
╭─root@localhost.localdomain ~
╰─➤ find / -perm -644 | head -3
/boot/grub
/boot/grub/splash.xpm.gz
/boot/.vmlinuz-3.10.0-514.el7.x86_64.hmac
3. exec: 执行命令
例子1:删除无属主的文件
╭─root@localhost.localdomain ~
╰─➤ find / -nouser -exec rm -rf {} \;
3:xargs
╭─root@localhost.localdomain /home
╰─➤ ls
du
╭─root@localhost.localdomain /home
╰─➤ touch {1..20}cjk
╭─root@localhost.localdomain /home
╰─➤ ls
10cjk 12cjk 14cjk 16cjk 18cjk 1cjk 2cjk 4cjk 6cjk 8cjk du
11cjk 13cjk 15cjk 17cjk 19cjk 20cjk 3cjk 5cjk 7cjk 9cjk
╭─root@localhost.localdomain /home
╰─➤ find /home -name "*cjk" | xargs rm -rf
╭─root@localhost.localdomain /home
╰─➤ ls
du
4.find可以使用-a和-o或!
-a 是而且的意思
-o 是或者的意思
例子1:找到所有权限是644的普通文件
╭─root@localhost.localdomain /home
╰─➤ find / -type f -a -perm 644 | head -3
/boot/grub/splash.xpm.gz
/boot/.vmlinuz-3.10.0-514.el7.x86_64.hmac
/boot/config-3.10.0-514.el7.x86_64
例子二:找到以du开头或以du结尾的普通文件
╭─root@localhost.localdomain /home
╰─➤ find / -name "du*" -o -name "*du" -a -type f
必要时可以用(),但是需要用"\"转义
删除日志文件(9号机桌面有命令)
find /alidata/www/logs/java/ebs/sys -mtime +7 -type f \( -name "task.log.*" -o -name "dubbo.log.*" -o -name "task-removeSnapshot.log.*" -o -name "service.log.*" -o -name "grandcanal_heartbeat,log,*" -o -name "grandcanal_thread.log.*" \) | xargs rm -f ;
5. user:指定属主
例子1:找到etc目录下属主是root的文件
╭─root@localhost.localdomain ~
╰─➤ find /etc -user root | head -3
/etc/
/etc/fstab
/etc/crypttab
6.group:指定属组
例子1:
╭─root@localhost.localdomain ~
╰─➤ find /etc -group root | head -3
/etc
/etc/fstab
/etc/crypttab
7. nouser:指定无属主
例子1:
╭─root@localhost.localdomain ~
╰─➤ find -nouser | head -5
./httpd-2.4.20
./httpd-2.4.20/.deps
./httpd-2.4.20/.gdbinit
./httpd-2.4.20/ABOUT_APACHE
./httpd-2.4.20/acinclude.m4
╭─root@localhost.localdomain ~
╰─➤ ll ./httpd-2.4.20/.deps
-rw-r--r--. 1 501 games 0 5月 23 20:33 ./httpd-2.4.20/.deps
8. nogroup:指定无属主
╭─root@localhost.localdomain ~
╰─➤ find -nogroup
9.type:指定文件类型
-type #文件类型
主要的文件类型:
f #普通文件
d #目录
b #块设备
s #套接字
c #字符设备
l #链接
p #管道
例子1:找到所有的链接文件
╭─root@localhost.localdomain ~
╰─➤ find / -type l | head -3
/dev/cdrom
/dev/snd/by-path/pci-0000:02:02.0
/dev/initctl
╭─root@localhost.localdomain ~
╰─➤ find -type l | head -3
./.oh-my-zsh/plugins/fedora/fedora.plugin.zsh
./.oh-my-zsh/plugins/go/go.plugin.zsh
./.oh-my-zsh/plugins/gradle/_gradle
╭─root@localhost.localdomain ~
╰─➤ ll ./.oh-my-zsh/plugins/fedora/fedora.plugin.zsh
lrwxrwxrwx. 1 root root 21 4月 19 11:20 ./.oh-my-zsh/plugins/fedora/fedora.plugin.zsh -> ../dnf/dnf.plugin.zsh
例子2: 找到所有普通文件
╭─root@localhost.localdomain ~
╰─➤ find -type f | head -3
./.bash_logout
./.bash_profile
./.bashrc
10. size:指定文件大小
例子1:找到大于100M的文件
╭─root@localhost.localdomain ~
╰─➤ find / -size +100M
/proc/kcore
/run/media/root/CentOS 7 x86_64/LiveOS/squashfs.img
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/usr/lib/locale/locale-archive
╭─root@localhost.localdomain ~
╰─➤ ls -lh /usr/lib/locale/locale-archive
-rw-r--r--. 1 root root 102M 4月 18 21:35 /usr/lib/locale/locale-archive
11. mtime: (modify time)指文件修改时间(天)
例子1:找到文件修改时间一天以上的
[root@ken ~]# find / -mtime +1 | head -3
/boot
/boot/efi
/boot/efi/EFI
[root@ken ~]# 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. atime:(access time)指定文件访问时间(天)
例子:找到访问时间1天以上的
[root@ken ~]# find / -atime +1 | head -3
/boot/grub2/device.map
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod
^C
[root@ken ~]# 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: –
13.ctime:(create time)指定创建时间(天)
例子1:指定文件创建时间一天以上
[root@ken ~]# find / -ctime +1 | head -3
/boot
/boot/efi
/boot/efi/EFI
^C^
[root@ken ~]# 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: –