grep_find

grep (针对文件)

grep过滤文本文件的内容(可以跟 > 或 | 一起用)
输出包含指定字符串的行

grep [选项] ‘匹配字符串’ 文本文件..
grep root /etc/passwd

-v 取反匹配
-i 忽略大小写

命令行 | grep [选项] ‘匹配字符串’
grep [选项] ‘匹配字符串’ 文本文件 > 文本文件
grep ‘seismic’ /usr/share/dict/words > /root/wordlist
文件/root/wordlist不要包含空行,并且其中所有行的内容必须是/usr/share/dict/words文件中原始行的准确副本。

^word #搜索以字符串word开头的行
word$ #搜索以字符串word结尾的行

grep ^root /etc/passwd #搜索以root开头的行
grep bash$ /etc/passwd #搜索以bash结尾的行

显示/etc/login.defs配置文件有效信息(去除注释行,去除空行),并将其复制到/opt/1.txt
grep -v ^# /etc/login.defs | grep -v ^$ > /opt/1.txt

grep经常和管道 | 一起使用

命令行 | grep [选项] '关键字'
cat /etc/passwd | grep root
----------------------------------------------------------------------------
find (查找文档)
根据预设条件,递归查找对应的目录或文件
find [目录] [条件1] [-a|-o] [条件2]..

-type 类型(f文件、d目录、l快捷方式) #只有ls -ld下-代表文件,其他是f代表文件。
-name 文档名称
-iname 根据名称查找,忽略大小写
-size +|-文件大小(小写k、M、G)
-user 用户
-group 所属组
-maxdepth 限制目录查找的深度(最大层数)
-mtime 根据文件修改时间,所有的时间都是过去时间

find /boot/ -type d #查找/boot目录下是目录的
find /boot/ -type f #查找/boot目录下是文件的
find /boot/ -type l #查找/boot目录下是快捷方式
------------------------------------------------------------------
wc 统计命令
wc -l /etc/passwd #统计行数
------------------------------------------------------------------
ls和find的区别
ls /etc/ -name "*.conf" | wc -l #只查询/etc/这个目录本身
find /etc/ -name "*.conf" | wc -l #递归查询
------------------------------------------------------------------
统计/etc目录下以".conf"结尾的有多少个?
find /etc/ -name "*.conf" | wc -l
find /root/ -name "nsd*" -type f #默认两个条件都满足

-size +10M #大于10M
-size -10M #小于10M

k(小写) M(大写) G(大写)
find /boot/ -size +10M
ls -lh /boot/initr*
--------------------------------------------------------------------
根据文档的所有者
-user

find / -user student
find / -user student -type d

/proc/:所占用的空间不是硬盘,而是内存。find查找时,提示此目录下报错是正常
---------------------------------------------------------------------
根据文档的所属组
-group

find /home/ -group student
---------------------------------------------------------------------
根据名称查找,忽略大小写
-iname

find /etc/ -iname "PASSWD"
---------------------------------------------------------------------
限制目录查找的深度(最大层数)
-maxdepth

find /etc/ -maxdepth 1 -name "*.conf"
find /etc/ -maxdepth 2 -name "*.conf"
---------------------------------------------------------------------
根据文件修改时间,所有的时间都是过去时间
-mtime

-mtime +10 #过去的 10天之前修改和创建的文档
-mtime -10 #过去的 10天之内修改和创建的文档
find /var/log/ -mtime +1000
----------------------------------------------------------------------
使用find命令的 -exec 操作
find .. .. -exec 处理命令 {} \;
优势:以 {} 代替每一个结果,逐个处理,遇 \; 结束

rm -rf /opt/*
find /etc/ -name "*tab" -exec cp {} /mnt/ \;
find /boot -size +10M -exec cp {} /opt/ \;
--------------------------------------------------------------------
使用find命令完成以下任务
找出所有用户 student 拥有的文件,把它们拷贝到 /root/findfiles/ 文件夹中。
mkdir /root/findfiles
find / -user student -type f -exec cp {} /root/findfiles/ \;
ls -A /root/findfiles/

猜你喜欢

转载自www.cnblogs.com/summer2/p/10787750.html