一、文件查找与打包压缩
grep: 文件内容过滤
[root@qfedu.com ~]# grep 'root' /etc/passwd #从/etc/passwd文件中过滤root字段
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
grep ^root /etc/passwd #过滤 /etc/passwd 文件中以root开头的行
grep login$ /etc/passwd #过滤 /etc/passwd 文件中以login结尾的行
grep -E "[A-Z][a-z]+"/etc/passwd #过滤/etc/passwd 文件大写字母开头的单词 +:表示在+前面的字符重复1到多次 例如:Username
grep -R "user" /etc #在etc/目录下的所有文件中,查找包含user内容的文件 -R是递归
查找命令
[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
查询命令和配置文件的位置
[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.权限
4.时间
按文件名
从根开始找文件
[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"
参数解释:
*:表示所有字符
按文件大小 -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:and
[root@qfedu.com ~]# find / -size -1M -o -size +80M #查找/下面小于1M或者大于80M的文件
-o:or
[root@qfedu.com ~]# find / -size -3M -a -name "*.txt" #查找/ 下面小于3M而且名字是.txt的文件
按时间查找
按时间找(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 . -mtime -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分钟之内
按文件类型
[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 /etc -type l #l链接
[root@qfedu.com ~]# find /dev -type b #b块设备
[root@qfedu.com ~]# find /dev/ -type b -name "sd*"
按文件权限
[root@qfedu.com ~]# find . -perm 644 #.是当前目录 精确查找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
找到后处理的动作 ACTIONS
[root@qfedu.com ~]# find /etc -name "ifcfg*" -exec cp -rf {} /tmp \; #exec命令对之前查找出来的文件做进一步操作----- 查找带ifcfg开头的文件复制到tmp下
[root@qfedu.com ~]# touch /home/test{1..20}.txt
[root@qfedu.com ~]# find /home/ -name test* -exec rm -rf {} \; #{}为前面查找到的内容,\; 格式
find使用xargs
[root@qfedu.com ~]# touch /home/test{1..20}.txt
[root@qfedu.com ~]# # find /home/ -name "test*" | xargs -i cp {} /tmp/ #找到之后删除处理xargs 参数传递
-exec和xargs的区别
-exec:参数是一个一个传递的,传递一个参数执行一次命令。
xargs:将前一个命令的标准输出传递给下一个命令,作为它的参数转换成下一个命令的参数列表。
===============
1、exec 每处理一个文件或者目录,它都需要启动一次命令,效率不好;
2、exec 格式麻烦,必须用 {} 做文件的代位符,必须用 \来转义; 作为命令的结束符,书写不便。
3、xargs不能操作文件名有空格的文件;
综上,如果要使用的命令支持一次处理多个文件,并且也知道这些文件里没有带空格的文件,那么使用 xargs比较方便; 否则,就要用 exec了。
**案例1: 分别找出test5 和除了test5的文件 **
[root@qfedu.com ~]# find /home/ -name *test5*
[root@qfedu.com ~]# find /home/ ! -name "test5*" # !--取反
实战练习:
1、查找/root目录下文件中包含logout的文件
grep -R "logout" /root 或者 grep -r "logout" /root
2、查找/etc/目录下所有的.txt 结尾的文件,并想办法得到有多少个文件被查出
find /etc/ -name "*.txt" > 123.txt cat -n 123.txt
第二种办法: find /etc/ -name "*.txt" | xargs -i echo {} >123.txt cat -n 123.txt
第三种方法: find /etc/ -name "*.txt" | wc -l
3、查询/etc目录下所有满权限的文件,并观察这些文件
find /etc -perm 777 -ls
4、查询系统中所有大于50M的文件
find /* -size +50M
5、查询/etc目录下所有.sh 结尾的且大于50字节的文件,并将这些文件复制到/tmp目录中
find /etc -name "*.sh" -a -size +50c | xargs -i cp {} /tmp/