一、grep
1、文件内容过滤
从/etc/passwd文件中过滤root字段
[root@qfedu.com ~]# grep 'root' /etc/passwd #
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
过滤文件中带有root的内容:
[root@linux-server ~]# grep 'root' /etc/passwd
过滤以root开头的行:^ --以什么开头
[root@linux-server ~]# grep '^root' /etc/passwd
过滤以bash结尾的行:$ --以什么结尾
[root@linux-server ~]# grep 'bash$' /etc/passwd
二、which
1、查找命令
[root@qfedu.com ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@qfedu.com ~]# which cd
/usr/bin/cd
[root@qfedu.com ~]# which rm
alias rm='rm -i'
/usr/bin/rm
三、whereis
1、查询命令位置
[root@qfedu.com ~]# whereis rpm
rpm: /usr/bin/rpm /usr/lib/rpm /etc/rpm /usr/share/man/man8/rpm.8.gz
[root@qfedu.com ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz
四、find
语法:
#find 路径 条件 跟条件相关的操作符 [-exec 动作]
路径:
1.默认不写路径时查找的是当前路径.
2.加路径。
条件:
1.指定的名称 -name
2.文件类型 - type
3.权限(-perm)
4.时间(-time)
1、按文件名(name)
从根开始找文件
[root@qfedu.com ~]# find / -name “file2” #从根开始找文件
/root/file2
/var/tmp/file2
[root@qfedu.com ~]# find /etc -name "ifcfg-ens33" #以名字的方式查找
[root@qfedu.com ~]# find /etc -iname "Ifcfg-ens33" #-i忽略大小写
熟用*通配符
[root@qfedu.com ~]# find /etc -iname "*.txt"
参数解释:
*:表示所有字符
案例1: 分别找出test5 和除了test5的文件
[root@qfedu.com ~]# find /home/ -name *test5*
[root@qfedu.com ~]# find /home/ ! -name "test5*" # !--取反
2、按文件大小 (size)
[root@qfedu.com ~]# find /etc -size +5M #大于5M
[root@qfedu.com ~]# find /etc -size 5M #等于5M
[root@qfedu.com ~]# find /etc -size -5M #小于5M
[root@qfedu.com ~]# find / -size +3M -a -size -5M #查找/下面大于3M而且小于5M的文件
-a:add
[root@qfedu.com ~]# find / -size -1M -o -size +80M #查找/下面小于1M或者大于80M的文件
-o:or
[root@qfedu.com ~]# find / -size -3M -a -name "*.txt" #查找/ 下面小于3M而且名字是.txt的文件
3、按时间查找(time)
按时间找(atime,mtime,ctime)
+ :之前
- : 之内
# 天
-atime= access访问时间
-mtime = modify修改时间 内容修改时间会改变
-ctime =change改变时间 属性修改时间会改变
# 分钟
-amin #
-mmin #
-cmin #
[root@qfedu.com ~]# find /opt -mtime +5 #修改时间5天之前
[root@qfedu.com ~]# find /opt -atime +1 #访问时间1天之前
[root@qfedu.com ~]# find . -ctime -2 #改变时间2天之内
[root@qfedu.com ~]# find . -amin +1 #访问时间在1分钟之前
[root@qfedu.com ~]# find /opt -amin -4 #访问时间在4分钟之内
[root@qfedu.com ~]# find /opt -mmin -2 #修改时间在2分钟之内
4、按文件类型(type)
[root@qfedu.com ~]# find /dev -type f #f普通文件
[root@qfedu.com ~]# find / -type f -size -1M -o -name "*.txt"
[root@qfedu.com ~]# find /dev -type d #d目录
[root@qfedu.com ~]# find /etc/ -type d -name "*.conf.d"
[root@qfedu.com ~]# find /dev -type l #l链接
[root@qfedu.com ~]# find /dev -type b #b块设备
[root@qfedu.com ~]# find /dev/ -type b -name "sd*"
[root@qfedu.com ~]# find /dev -type c #c字符设备
[root@qfedu.com ~]# find /dev -type s #s套接字
5、按文件权限(perm)
是当前目录 精确查找644
[root@qfedu.com ~]# find . -perm 644 #.
[root@qfedu.com ~]# find /usr/bin -perm -4000 #包含set uid
[root@qfedu.com ~]# find /usr/bin -perm -2000 #包含set gid
[root@qfedu.com ~]# find /usr/bin -perm -1000 #包含sticky
6、动作(actions)
-exec :格式 {} \; 花括号+空格+\+;
xargs:格式 -i {} -i 使后面的花括号生效
exec命令对之前查找出来的文件做进一步操作----- 查找带ifcfg开头的文件复制到tmp下
{}为前面查找到的内容,\; 格式
[root@qfedu.com ~]# find /etc -name "ifcfg*" -exec cp -rf {} /tmp \; #
[root@qfedu.com ~]# touch /home/test{1..20}.txt
[root@qfedu.com ~]# find /home/ -name test* -exec rm -rf {} \; #
找到之后删除处理xargs 参数传递、
[root@qfedu.com ~]# find . -name "yang*.txt" |xargs rm -rf #
[root@qfedu.com ~]# touch /home/test{1..20}.txt
[root@qfedu.com ~]# # find /home/ -name "test*" | xargs -i cp {} /tmp/
7、-exec和xargs的区别
-exec:参数是一个一个传递的,传递一个参数执行一次命令。
xargs:将前一个命令的标准输出传递给下一个命令,作为它的参数.
它的任务就是将输入行转换成下一个命令的参数列表。
===============
1、exec 每处理一个文件或者目录,它都需要启动一次命令,效率不好;
2、exec 格式麻烦,必须用 {} 做文件的代位符,必须用 \来转义; 作为命令的结束符,书写不便。
3、xargs不能操作文件名有空格的文件;
综上,如果要使用的命令支持一次处理多个文件,并且也知道这些文件里没有带空格的文件,
那么使用 xargs比较方便; 否则,就要用 exec了。