文件查找 find
语法格式:
find 位置/路径 条件 值 动作
条件:
-name文件名字
-type文件类型
-size文件大小 +5M 大于5M 5M 在5M范围内
.....
atime 访问时间 ctime 改变时间 mtime 修改时间
1.整理三个时间分别的含义
2.那么命令可以影响这三个时间
-a and 条件都要满足
-o or 条件满足其中一个
\( \) \:符号回归本意 转义符
查找的条件:
-name
-size
-mtime
-user
-group
-type 按照类型查找 普通文件 f 文件类型 d s p l b c
-perm 指定 644 755 400 -644 包含
-regex 正则
单个条件
多个条件 -a 并且/和 -o 或者
动作: 默认-print 显示详细信息 -ls
注意:在多个条件的时候 无论使用-a 还是使用-o 把条件都用\( \) 括起来
查找所有以.sh结尾的文件在etc目录下:
find /etc -name "*.sh"
查找系统中所有zhangsan的文件:
find / -user zhangsan
查找系统中所有以8开头的文件和以9结尾的文件:
find / -name "8*" -o -name "*9"
正则: .* 任意多个字符 [0-9] [a-z] [0-9a-z] [A-Z] [0-9A-Z] [a-zA-Z] + 出现多次
[0-9]+
-not ! 取反
查找opt目录除了名为file1的文件
[root@xingdian opt]# find /opt/ ! -name "file1"
[root@xingdian opt]# find /opt/ -not -name "file1"
动作
-ls
[root@xingdian opt]# find /opt/ -name "file1" -delete
-delete
-exec
-ok
文件操作: mv cp rm(去掉) 压缩
查找opt目录下所有以2结尾的文件移动到/mnt目录下
[root@xingdian opt]# find /opt/ -name "*4" -ok mv {} /mnt \;
[root@xingdian opt]# find /opt/ -name "*4" -ok 文件操作命令(cp mv rm) {承接查到的内容} 目的地 \;
-ok和-exec 的区别 -ok需要人工确认 y
[root@xingdian opt]# find /opt/ -name "*2" |xargs -i mv {} /mnt/
[root@xingdian opt]# find /opt/ -name "*5" |xargs -I {} mv {} /tmp
这些都可以
文件打包压缩与解压缩
压缩包
Windows: .rar .zip
Linux:.zip .tar.gz .tar.bz2 .tar.xz .tgz
tar包
之所以会得到不同格式的压缩包,是因为调用的压缩方式不同
.tar.gz gzip z
.tar.bz2 bzip2 j
.tar.xz xz J
打包压缩:tar cf 路径/压缩包的名字 压缩的文件
-exclude-tag=FILE 除 FILE 自身外,排除包含 FILE
的目录中的内容
--exclude-tag-all=FILE 排除包含 FILE 的目录
--exclude-tag-under=FILE 排除包含 FILE 的目录中的所有内容
.tar.gz
解压缩:tar xf 压缩包的名字
指定位置解压 -C
[root@xingdian ~]# tar xf logstash-7.3.2.tar.gz -C /opt/
zip包
解压缩: unzip 压缩包名字 (yum -y install unzip)
指定位置解压缩:[root@xingdian ~]# unzip -d /opt/ nextcloud-22.0.0.zip
案例:
查到/etc目录下所有以.conf结尾的文件并进行压缩,压缩到/opt目录下命名为conf.tar.gz
查找和压缩同时进行使用rf这种。
[root@xingdian opt]# find /etc -name "*.conf" |xargs -i tar rf /opt/conf-1.tar.gz {}
用exec时rf得加-
扩展:
nginx 服务 日志文件 access.log 日志轮转 对日志文件切割 旧日志文件打包.gz
.gz
如果没有gunzip,使用yum -y install gzip
gunzip access.log.gz
案例:拷贝海量小文件
scp -r 源文件 目的地
[root@xingdian ~]# scp -r nextcloud 10.9.12.209:/opt/ 文件多速度慢
[root@xingdian ~]# tar cf nextcloud.tar.gz nextcloud
[root@xingdian ~]# scp -r nextcloud.tar.gz 10.9.12.209:/opt/