实验环境为:centOs 7 + XShell
一、查找指令
1. 按名字查找 —— find 指令
find /targetFileName -name findedFilename
示例: 在 / home
文件夹下查找 test.txt
find /home -name test.txt
2. 搜索查找类 —— grep 指令和管道符号 |
grep 表示过滤查找,管道符表示将前一个命令的处理结果输出给后面的命令处理
常用选项:
-n 表示输出行号
grep [选项] 查找内容 源文件
实例: 在 hello.txt (在 home 文件夹下)文件中,查找 yes 所在行,并返回行号
方法一: 通过管道将 cat 的内容输入给 grep
cat hello.txt | grep "yes"
方法二:直接在后面加上要查找的文件内容
grep -n "yes" hello.txt
二、压缩和解压指令
1. gzip / gunzip 指令
示例:
- 把 home 文件下的 hello.txt 压缩
gzip hello.txt
- 把 hello.txt.gz 解压
gunzip hello.txt.gz
2. zip / unzip 指令
zip 用于压缩,unzip 用于解压
常用选项:
-r: 递归压缩,即压缩目录
-d<目录> : 指令解压后的存放目录
示例:
- 将 /home 下的所有文件压缩为 myhome.zip
将 home 文件夹及其子文件夹都压缩了
zip -r myhome.zip /home/
- 将 myhome.zip 解压到 /opt/tmp 目录下
unzip -d /opt/temp myhome.zip
3. tar 指令
打包指令,最后打包后的文件是 .tar.gz 文件
常用选项:
-c : 产生 .tar 打包文件
-v: 显示详细信息
-f: 指定压缩后的文件名
-z: 打包的同时压缩
-x: 解包 .tar 文件
示例:
- 压缩多个文件,将 /home/pig.txt 和 /home/cat.txt 压缩成 pc.tar.gz
注意:多个文件间用空格隔开
tar -zcvf pc.tar.gz big.txt cat.txt
- 将 /home 的文件夹压缩为 myhome.tar.gz
tar -zcvf myhome.tar.gz /home/
- 将 pc.tar.gz 解压到当前目录
tar -zxvf pc.tar.gz
- 将 myhome.tar.gz 解压到 /opt/tmp2 目录下
tar -zxvf /home/myhome.tar.gz -C /opt/tmp2