1. 打包压缩
- 为什么要打包压缩
- 把一堆文件放在一起,移动方便
- 压缩后节省空间,文本节省2/3
- 实现备份功能
- 打包压缩工具:tar,gzip,unzip,zip
1.1. tar
打包压缩(一般用这个扩展名.tar.gz)
1.1.1 tar打包
- 打包组合:zcvf
- 语法:
tar zcvf 压缩包的名字 要打包的内容
- 例子:将ect目录下的文件打包放到 当前目录中的etc.tar.gz中(没有就创建)
tar zcvf etc.tar.gz /etc/
- 细节:打包后会去掉目录根的符号
/etc
变成etc
(如果不这样替换会在解压时,替换了可能更新的源文件)
1.1.2. tar解包
- 选项组合
- 解包到当前目录:
tar zxvf 包名
- 解包到指定目录:
tar zxvf 包名 -C 指定目录
- 解包时的常用方法
- 不解包看内容:
tar tf 包
- 让打包的路径带时间(备份):
反引号
和$()
可以把命令放到里边- 方法
[root@localhost ~]# tar zcvf etc_$(date +%F).tar.gz /etc/
- 结果
[root@localhost ~] ls
anaconda-ks.cfg etc.tar.gz oldboy.txt
etc_2023-07-21.tar.gz m1 test
1.2 gzip
打包压缩(压缩和解压后都删除原文件)
- 打包到当前目录:
gzip a.txt
- 查看包内内容:
zcat a.txt.gz
- 多个文件打包压缩:
gzip -r oldboy
- 解压:
gzip -d a.txt.gz
1.3 zip
解压缩
- 用处:解压Windows zip文件缩到当前目录
- 语法:
unzip -o oldboy.zip
2. 查找
2.1 which
查找二进制命令
- 语法:
which 命令
- 特点:速度快
- 原理:从$PATH里面路径下查找的
[root@localhost ~] echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
- 拓展
- 执行命令原理
- 查看命令在$PATH中的目录下有没有,没有则报错
- 如果有则执行命令,命令由bash解释器解析
- 解析后就看到显示的结果
- 增加命令
- 在~下增加命令
- cd ~
vim fff
:回车编辑增加内容echo “i am oldboy”,然后保存。chmod +x fff
./fff
:可以输出结果fff
:没有结果
- 把命令放到PATH变量里边的路径中
cp fff /usr/bin/
fff
:可以输出结果rm -f /usr/bin/fff
fff
:没有结果
- 把脚本路径放到PATH变量中
export PATH="$PATH:~"
fff
:找~里面的fff,所以可以输出结果
2.2 find
命令查找文件
- 特点:遍历查找,慢
- 语法:
find 路径 选项1 选项2
2.2.1 -name
按名字查找
- 全名查找:
find 限定文件路径 -name "hosts"
- 模糊查找:
find 限定文件路径 -name "h*ts"
- 按扩展名查找:
find 限定文件路径 -name "*.txt"
2.2.2 -type
按类型查找
参数 | 英文 | 作用 |
---|
b | block | 块设备,硬盘 |
c | character | 字符设备,串口设备 |
d | directory | 目录 |
f | regular file | 文件 |
l | symbolic link | 软链接,快捷方式 |
s | socket | 套接字文件 |
- 练习
- 查找~下的文件
[root@localhost ~] find ~ -type f
/root/.bash_logout
/root/.bash_profile
/root/.cshrc
- 查找~下的目录
[root@oldboyedu ~] find ~ -type d
/root
/root/oldboy
- 查找~下的文件,且txt结尾
[root@oldboyedu ~] find ~ -type f -name "*.txt"
/root/a.txt
2.2.3 -size
按大小搜索文件
- 大于某个大小的文件
find 文件 -size +大小
- 等于某个大小的文件
find 文件 -size 大小
- 小于某个大小的文件
find 文件 -size -大小
- 单位
大小单位符号 | 单位含义 |
---|
c | 字符 |
w | 单词 |
b | 字节 |
k | 千兆字节 |
M | 兆字节 |
G | G |
2.2.4 -mtime
按修改时间查找
- n天之前
find -mtime +n
- n天
find -mtime n
- 最近七天
find -mtime -n
2.2.5 其他参数
find 文件 -user 用户名
find 文件 -perm 数字
:与权限有关
2.3 find命令与其他命令的结合
2.3.1 find对找到的东西进行处理(修改、删除)
- 方法一:执行动作
-exec
- 语法:
find 路径 -mtime +7 -exec rm -f {} \;
- {}:代表find所查找到的内容
- 特点:效率低
- 原理:相当于一个一个的删,效率低
find找到文件以后
rm -f /data/file_2023-07-01
rm -f /data/file_2023-07-02
.....
rm -f /data/file_2023-07-22
- 方法二:find+xargs+rm组合删除
- 语法:
find /data -type f -mtime -7|xargs rm -f
- 原理:相当于一个rm删除所有发现的文件
[root@oldboyedu ~] find /data -type f -mtime -7|xargs rm -f
rm -f /data/file_2023-07-24 /data/file_2023-07-25 /data/file_2023-07-26 /data/file_2023-07-27 /data/file_2023-07-28 /data/file_2023-07-29 /data/file_2023-07-30
2.3.2 find+cp文件复制find+mv同理文件移动
- 方法一(利用大括号移动find出文件的位置)(xargs -i是将找到的内容放到大括号中)
find /data -name "file*" -mtime +5|xargs -i cp {} /tmp
- 方法二(利用cp 的参数调整cp语法源和目标位置顺序)
find /data/ -type f -mtime +3|xargs cp -t /tmp
2.3.3 对find找到的东西打包压缩
- 查找/sbin下所有以ip开头的文件打包
find /sbin/ -type f -name "ip*"|xargs tar zcvf ip.tar.gz
2.3.4 使用find找到含有某个字符串的文件
find / -type f -mtime -7|xargs grep "oldboy"
2.3.5 find的逻辑操作符
- 并且 and -a 默认多个条件中间就是-a 省略了。
- 或者 or -o 写法:
find /etc -type f -o -mtime -7
- 非 ! 写法:
find /etc ! -type f -o !-mtime -7