grep 目标 文件: 在文件中查找目标
[root@S opt]# grep root /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@S opt]#
grep -i 忽略大小写
[root@S opt]# grep -i ROOT /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@S opt]#
grep -v 取反匹配
不显示含有目标内容的行
grep -v root /etc passwd -----显示/etc/passwd文件中不包含root的行
grep -n 显示在原文中所处的行号
[root@S opt]# grep -n root /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin [root@S opt]#
grep ^word 以word开头的行
[root@S opt]# grep ^root /etc/passwd root:x:0:0:root:/root:/bin/bash [root@S opt]#
grep word$ 以word结尾的行
[root@S opt]# grep bash$ /etc/passwd root:x:0:0:root:/root:/bin/bash student:x:1000:1000:Student User:/home/student:/bin/bash harry:x:1001:1001::/home/harry:/bin/bash [root@S opt]#
grep ^$ 匹配空行
grep -v ^$ 去除空行
[root@S opt]# grep -v ^$ /etc/default/useradd # useradd defaults file GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/bash SKEL=/etc/skel CREATE_MAIL_SPOOL=yes [root@S opt]#
去除配置文件的注释行/空行 显示行号写入到new_file中
grep -v ^# /etc/conf | grep -v ^$ | cat -n > new_file
find:
find dir_name -type [d f l] : 在目录中查找目录 文件 链接
[root@S /]# find /boot/ -type d /boot/ /boot/grub2 /boot/grub2/themes /boot/grub2/themes/system /boot/grub2/i386-pc /boot/grub2/locale /boot/grub2/fonts /boot/grub [root@S /]#
find dir_name -name '文档名称‘
[root@S /]# find /etc/ -name 'passwd' /etc/passwd /etc/pam.d/passwd [root@S /]#
[root@S /]# find /etc/ -name 'passw*' /etc/passwd /etc/passwd- /etc/pam.d/passwd /etc/pam.d/password-auth-ac /etc/pam.d/password-auth [root@S /]#
find dir_name -name xxx -type l 多条件查找
# 包含‘nsd'的目录
[root@S /]# find /root/ -name 'nsd*' -type d /root/nsd01 /root/nsd02 [root@S /]#
find dir_name -size +|- 文件大小(k,M,G)
查找大于10M的文件
[root@S /]# find /boot/ -size +10M /boot/initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img /boot/initramfs-3.10.0-123.el7.x86_64.img [root@S /]# ls -lh /boot/initramfs-* -rw-r--r--. 1 root root 30M 5月 7 2014 /boot/initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img -rw-r--r--. 1 root root 29M 5月 7 2014 /boot/initramfs-3.10.0-123.el7.x86_64.img [root@S /]#
find dir_name -user 用户名
查找属于某用户的文件
[root@S /]# find / -user student find: ‘/proc/30816/task/30816/fd/6’: 没有那个文件或目录 find: ‘/proc/30816/task/30816/fdinfo/6’: 没有那个文件或目录 find: ‘/proc/30816/fd/6’: 没有那个文件或目录 find: ‘/proc/30816/fdinfo/6’: 没有那个文件或目录 /var/spool/mail/student /home/student /home/student/.bash_logout /home/student/.bash_profile /home/student/.bashrc /home/student/.ssh /home/student/.ssh/authorized_keys /home/student/.config /home/student/.config/gnome-initial-setup-done /home/student/.config/monitors.xml /home/student/.config/abrt /home/student/.cache/abrt/lastnotification /home/student/.bash_history [root@S /]#
-maxdepth n 限制查找的深度(最大层数)
[root@S /]# find /etc/ -maxdepth 2 -name '*.conf' /etc/yum/version-groups.conf /etc/yum/yum-cron-hourly.conf /etc/yum/yum-cron.conf /etc/audit/auditd.conf /etc/fonts/fonts.conf /etc/prelink.conf.d/fipscheck.conf /etc/prelink.conf.d/grub2.conf /etc/prelink.conf.d/libreswan-fips.conf /etc/tmpfiles.d/tuned.conf /etc/tmpfiles.d/libstoragemgmt.conf
-mtime 根据文件修改时间查找
-mtime +10 #10天之前 -10#10天之内
1000天之前的文件
[root@S /]# find /var/log/ -mtime +1000 /var/log/tallylog /var/log/btmp /var/log/ppp /var/log/tuned /var/log/qemu-ga /var/log/spooler /var/log/ovirt-guest-agent /var/log/rhsm /var/log/audit
[root@S /]# ls -l //var/log/btmp
-rw-------. 1 root utmp 0 5月 7 2014 //var/log/btmp
[root@S /]#
find ... -exec 处理命令 {} .. ;
[root@S /]# find /boot/ -size +10M /boot/initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img /boot/initramfs-3.10.0-123.el7.x86_64.img [root@S /]# find /boot/ -size +10M -exec cp {} /opt ; [root@S /]# ls /opt/ initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img test initramfs-3.10.0-123.el7.x86_64.img [root@S /]#
#查找用户student的文本文件并复制到/root/findfiles
[root@S /]# mkdir /root/findfiles [root@S /]# find / -user student -type f -exec cp {} /root/findfiles/ ; find: ‘/proc/31255/task/31255/fdinfo/6’: 没有那个文件或目录 find: ‘/proc/31255/fdinfo/6’: 没有那个文件或目录 [root@S /]# ls -A /root/findfiles/ authorized_keys .bash_profile lastnotification .bash_history .bashrc monitors.xml .bash_logout gnome-initial-setup-done student [root@S /]#