find 按条件查找文件/目录所在位置
• 根据预设的条件递归查找对应的文件 可以查找子目录
– find [目录] [条件1] [-a|-o]连接符 [条件2]……
-a 两者皆满足,也可以省略
-o 一般很少用
– 常用条件表示:
-type 类型(f 文本文件、d 目录、l 快捷方式)
-name "文档名称" 支持通配符
-size +|-文件大小(k、M、G) 1k 1M 1G 不能识别
-user 用户名
-group 组名
-mtime 修改时间
请查找/etc目录下以.conf结尾的文本文件?(不包含子目录)
[root@localhost ~]# ls /etc/*.conf
请查找/etc目录下以.conf结尾的文本文件?(包含子目录)
[root@localhost ~]# find /etc/ -name "*.conf"
[root@localhost ~]# find /boot/ -type l #查找快捷方式
[root@localhost ~]# find /boot/ -type f #查找文件包括隐藏文件
[root@localhost ~]# find /boot/ -type d #find查找目录
/boot/
/boot/efi
/boot/efi/EFI
/boot/efi/EFI/centos
/boot/grub2
/boot/grub2/i386-pc
/boot/grub2/locale
/boot/grub2/fonts
/boot/grub
[root@localhost ~]# find /boot/ -name "vm*"# 支持通配符
/boot/vmlinuz-3.10.0-693.el7.x86_64
/boot/vmlinuz-0-rescue-7e242ae6581648e9bf582a14fb14e442
[root@localhost ~]# find /etc/ -name "passwd" #严格匹配
/etc/pam.d/passwd
/etc/passwd
[root@localhost ~]# mkdir /root/install
[root@localhost ~]# touch /root/install.log
[root@localhost ~]# touch /root/install.txt
[root@localhost ~]# find /root -name "install*"
/root/.cache/gnome-software/3.22/flatpak/installation-tmp
/root/install
/root/install.log
/root/install.txt
[root@localhost ~]# find /root -name "install*" -a -type d
/root/.cache/gnome-software/3.22/flatpak/installation-tmp
/root/install
[root@localhost ~]# find /root -name "install*" -type f #默认是连接-a 两者皆满足
/root/install.log
/root/install.txt
[root@localhost ~]# find /boot/ -size +10M #
[root@localhost ~]# find /boot/ -size -10M
[root@localhost ~]# find / -user student #按照用户名查找
find: ‘/proc/44903/task/44903/fd/6’: 没有那个文件或目录
find: ‘/proc/44903/task/44903/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/44903/fd/6’: 没有那个文件或目录
find: ‘/proc/44903/fdinfo/6’: 没有那个文件或目录
/var/spool/mail/student
^C
[root@localhost ~]# find /home/ -group student #按照组名查找
/home/student
/home/student/.mozilla
/home/student/.mozilla/extensions
/home/student/.mozilla/plugins
/home/student/.bash_logout
/home/student/.bash_profile
/home/student/.bashrc
[root@localhost ~]# find /home/ -user student
/home/student
/home/student/.mozilla
/home/student/.mozilla/extensions
/home/student/.mozilla/plugins
/home/student/.bash_logout
/home/student/.bash_profile
/home/student/.bashrc
[root@localhost ~]# find /root/ -mtime +100 #按照时间来查找
[root@localhost ~]# find /root/ -mtime -10
[root@localhost ~]# find /var/log -mtime +90
********************************************************************
[root@localhost ~]# wc /etc/passwd
41 79 2101 /etc/passwd
行数 单词数 文件大小
[root@localhost ~]# wc -l /etc/passwd #统计行数
41 /etc/passwd
[root@localhost ~]# find /etc/ -name "*tab"
/etc/fstab
/etc/crypttab
/etc/mtab
/etc/selinux/targeted/active/modules/100/updfstab
/etc/anacrontab
/etc/crontab
/etc/inittab
/etc/rwtab
/etc/statetab
[root@localhost ~]# find /etc/ -name "*tab" | wc -l #统计tab结尾文件个数
9
处理find找到文件
根据条件查找并处理结果
-使用find命令的 --exec 额外操作
– find .. .. -exec 处理命令 {} \; {}:替代每一个查找结果 \;:处理操作结束
优势:以{}代替前面find查询到的每一个结果,逐个处理,遇到 \; 结束
[root@localhost ~]# rm -rf /opt/*
[root@localhost ~]# ls /opt/
[root@localhost ~]# find /boot/ -name "vm*" #查找以vm开头文件
/boot/vmlinuz-3.10.0-693.el7.x86_64
/boot/vmlinuz-0-rescue-7e242ae6581648e9bf582a14fb14e442
[root@localhost ~]# find /boot/ -name "vm*" -exec cp {} /opt \; #将查找到vm开头文件,复制到opt目录下
[root@localhost ~]# ls /opt/
vmlinuz-0-rescue-7e242ae6581648e9bf582a14fb14e442
vmlinuz-3.10.0-693.el7.x86_64
[root@localhost ~]# find /boot/ -size +10M
/boot/initramfs-0-rescue-7e242ae6581648e9bf582a14fb14e442.img
/boot/initramfs-3.10.0-693.el7.x86_64.img
[root@localhost ~]# find /boot/ -size +10M -exec cp -r {} /opt/ \;
[root@localhost ~]# ls /opt
initramfs-0-rescue-7e242ae6581648e9bf582a14fb14e442.img
initramfs-3.10.0-693.el7.x86_64.img
vmlinuz-0-rescue-7e242ae6581648e9bf582a14fb14e442
vmlinuz-3.10.0-693.el7.x86_64
[root@localhost ~]# find /etc/ -name "*tab"
/etc/fstab
/etc/crypttab
/etc/mtab
/etc/selinux/targeted/active/modules/100/updfstab
/etc/anacrontab
/etc/crontab
/etc/inittab
/etc/rwtab
/etc/statetab
[root@localhost ~]# find /etc/ -name "*tab" -exec cp -r {} /mnt/ \;
[root@localhost ~]# ls /mnt/
anacrontab crypttab inittab passwd shadow student whj
crontab fstab mtab rwtab statetab updfstab
[root@localhost ~]# mkdir /root/findfiles
[root@localhost ~]# find / -user student -type f -exec cp -r {} /root/findfiles/ \;
网友评论