linux文件内容过滤、文件查找

162 阅读1分钟

grep:文件内容过滤

grep ‘root’ /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
​
​
cat /etc/passwd | grep 'root'

查找命令

which cd
/usr/bin/cd
​
which ls
alias ls='ls --color=auto'
        /usr/bin/ls

一、find详解:文件查找,针对文件名

语法:
find 路径 条件 跟条件相关的操作符  [-exec 动作]
路径:
1.默认不谢路径时查找的是当前路径
2.加路径
条件:
1.指定的名称 -name
2.文件大小 -size

1.1.按文件名

从根开始找文件
find / -name 'root'
​
以名字的方式在etc文件夹查找
find /etc -name 'idcfg-ens33'
​
-iname忽略大小写
find /etc -iname 'IFCFG-ENS33'
​
用* 通配符
find /etc -name '*.txt'

1.2.按文件的大小

大于5m
find /etc -size +5M
​
等于5m
find /etc -size 5M
​
小于5M
find /etc -size -5M
​
查找/下面大于3M而且小于5M的文件
find / -size +3M -a -size -5M
​
查找/下面小于1M或者大于80M的文件
find / -size -1M -o -size +80M
 
查找/ 下面小于3M而且名字是.txt的文件
find / -size -3M -a -name '*.txt'

1.3.找到后处理的动作 ACTIONS

#exec命令对之前查找出来的文件做进一步操作-----  查找/var/log下大于1m的文件,复制到/root/下
find /var/log -size +1M -exec cp {} /root/ ;
​
{}为前面查找到的内容    ; 格式
touch /home/test{1..20}.txt
find /home/ -name test* -exec rm -rf {} ; 

案例1:分别找出test5 和除了test5的文件

find /home -name *test5*
​
! 取反
find /home -name ! *test5*