作用:1.减小占用空间大小 2.整合分散的数据
• 归档的含义
将许多零散的文件整理为一个文件 文件总的大小基本不变
• 压缩的含义
按某种算法减小文件所占用空间的大小 恢复时按对应的逆向算法解压• 常见的压缩格式及命令工具:
.gz -> gzip 压缩快,用的多
.bz2 -> bzip2 压缩快,用的多
.xz -> xz 压缩慢,用的少
• tar 集成备份工具– -c:创建归档
– -x:释放归档
– -f:指定归档文件名称,必须在所有选项的最后
– -z、-j(勾)、-J:调用 .gz、.bz2、.xz 格式工具进行处理
– -t:显示归档中的文件清单
– -C:指定释放路径
压缩之后原有数据仍在
-f 选项必须在选项最后
tar 制作压缩包(tar打包)格式:tar 选项 /路径/压缩包名字 /源数据……. (不写路径会在当前 下创建)
]# tar -zcf /root/xixi.tar.gz /etc/passwd /home
其中.tar.gz表示是gzip格式压缩的压缩包 也可写成.tgz
不写.tar.gz扩展名是黑色,但仍是tar包
[root@student ~]# tar -zcf /root/xixi.tar.gz /etc/passwd /home tar: 从成员名中删除开头的“/” [root@student ~]# ls /root xixi.tar.gz 模板 图片
[root@student ~]# tar -jcf /root/hehe.tar.bz2 /etc/passwd /home tar: 从成员名中删除开头的“/” [root@student ~]# ls /root hehe.tar.bz2 xixi.tar.gz 视频 下载
[root@student ~]# tar -Jcf /root/gg.tar.xz /etc/passwd /home tar: 从成员名中删除开头的“/” [root@student ~]# ls /root gg.tar.xztar 释放压缩包(tar解包)
tar 选项 /路径/压缩包名字 选项 /释放的位置
– -x:释放归档
– -f:指定归档文件名称,必须在所有选项的最后
-C:指定释放路径
会直接覆盖原有同名数据
[root@student ~]# mkdir /nsd10 [root@student ~]# tar -xf /root/xixi.tar.gz -C /nsd10 [root@student ~]# ls /nsd10 etc home 压缩时会将目录一块压缩 释放后压缩包仍存在
[root@student ~]# tar -tf /root/xixi.tar.gz 查看压缩包内容
二、重定向(重新定向命令的输出)将前面命令的输出,作为内容,写入到后面的文件
>:覆盖重定向
>>:追加重定向
[root@A /]# head -5 /etc/passwd > /opt/p.txt [root@A /]# cat /opt/p.txt [root@A /]# hostname [root@A /]# hostname >> /opt/p.txt [root@A /]# cat /opt/p.txt [root@A /]# echo 123456 [root@A /]# echo 123456 > /opt/p.txt [root@A /]# cat /opt/p.txt [root@A /]# cat /etc/hostname [root@A /]# echo nb.tedu.cn > /etc/hostname [root@A /]# cat /etc/hostname三、管道(操作符号 |)
作用:将前面命令的输出,传递给后面命令,作为后面命令的参数
必须前面命令有输出才能支持管道操作,并且不是所有输出支持管道操作,只要是双参数以上命令不支持操作(前面的命令有输出,管道后面的操作可能关联)
]# head -4 /etc/passwd | tail -1 ]# head -8 /etc/passwd | tail -1 ]# cat -n /etc/passwd | head -8 | tail -1 ]# ifconfig | head -2 ]# head -12 /etc/passwd | tail -5 ]# head -12 /etc/passwd | tail -5 | cat -n ]# cat -n /etc/passwd | head -12 ]# cat -n /etc/passwd | head -12 | tail -5 ]# cat -n /etc/passwd | head -12 | tail -5 > /opt/pa.txt ]# cat /opt/paxt四、grep高级使用
作用:从文本文件内容中,过滤关键字符串
]# grep root /etc/passwd
]# grep -v root /etc/passwd #取反匹配
]# grep ^root /etc/passwd #以root开头
]# grep bash$ /etc/passwd #以bash结尾
^$:表示空行
]# cat /etc/default/useradd ]# grep -v ^$ /etc/default/useradd 表示将空行去掉
-v 表示取反
Linux中大多数配置文件内容,以#开头的行为注释行
显示配置文件有效信息(去除以#开头的注释行和去除空行)
]# grep -v ^# /etc/login.defs ]# grep -v ^# /etc/login.defs | grep -v ^$ 取反 #(注释)开头 ^$ 空行 去掉注释 和 空行 ]# grep -v ^# /etc/login.defs | grep -v ^$ > /opt/log.txt ]# cat /opt/log.txt五、 find精确查找
格式:find [目录] [条件1]
隐藏目录也会被发现
– 常用条件表示:
-type 类型(类型(f文件、d目录、l快捷方式) -name "文档名称" -size +|-文件大小(k、M、G) -user 用户名 -mtime 修改时间
-type 类型(f文本文件、d目录、l快捷方式
[root@A /]# find /boot -type d
[root@A /]# find /opt -type d
[root@A /]# find /etc -type l
[root@A /]# find /boot -type f
统计多少行
[root@cpt ~]# wc -l /etc/passwd 45 /etc/passwd [root@cpt ~]# find /boot/ -type d |wc -l 9
*-name “文档名称”*
]# find /etc/ -name "passwd" ]# find /etc/ -name "*tab" ]# find /etc/ -name "*.conf" ]# find /etc/ -name "*tab" | wc -l ]# find /etc/ -name "*.conf" | wc -l ]# find /etc/ -name "*.conf" | cat -n
/proc:内存的数据,不占用硬盘空间
find只能查询硬盘里的数据
find地毯式搜索,全部查完才结束动作
六、 find高级使用处理find找到的数据,每查找的一个就传递一次:每查到一次就传到{}执行一次。(tar包存在覆盖,故只能压缩find结果中的最后一个)
– find [范围] [条件] -exec 处理命令 {} ;
-exec额外操作的开始
{} 永远表示前面find查找的结果
; 额外操作的结束
]# find /boot/ -size +10M
]# find /boot/ -size +10M -exec cp {} /mnt ;
]# ls /mnt/
七、命令模式操作
• 光标跳转
| 操作类型 | 按键指令 | 用 途 |
|---|---|---|
| 移动光标 | á、â、ß、àà | 上、下、左、右 |
| 光标行内跳转 | Home 键 或 ^、数字 0 | 跳转到行首 |
| End 键 或 $ 键 | 跳转到行尾 | |
| 全文翻页 | PgUp 键、PgDn 键 | 向上翻页、向下翻页 |
| 光标行间跳转 | 1G 或 gg | 跳转到文件的首行 |
| G | 跳转到文件的末尾行 |
]# cp /etc/passwd /opt/pass.txt
]# vim /opt/pass.txt
- 复制/粘贴/删除
| *操作类型* | *按键指令* | *用 途* |
|---|---|---|
| 复制 | yy、3yy | 复制光标处的一行、3行 |
| 粘贴 | p、P | 粘贴到光标处之后、之前 |
| 删除 | x 或 Delete键 | 删除光标处的单个字符 |
| dd、3dd | 删除光标处的一行、#行 | |
| d^ | 从光标处之前删除至行首 | |
| d$或D(大写) | 从光标处删除到行尾 |
小写的u为 撤销
• *查找/撤销/保存*
| *操作类型* | *按键指令* | *用 途* |
|---|---|---|
| 文本查找 | /a | 向后查找字符串“a” |
| n、N | 跳至后/前一个结果 | |
| 撤销编辑 | u | 撤销最近的一次操作 |
| U | 撤销对当前行的所有修改 | |
| Ctrl + r | 取消前一次撤销操作 | |
| 保存退出 | ZZ(大写) | 保存修改并退出 |
u *末行模式操作*
| :r /etc/filesystems | 读入其他文件内容 |
|---|---|
]# echo 123456 > /opt/a.txt ]# echo hahaxixi > /opt/c.txt ]# vim /opt/c.txt 末行模式下 :r /opt/a.txt 末行模式下 :r /etc/passwd
• *字符串替换*
| *操作类型* | *设置指令* | *用 途* |
|---|---|---|
| 行内替换 | /root/new | 替换光标所在行第一个“root” |
| /root/new/g | 替换光标所在行所有的“root” | |
| 区域内替换 | :1,10s/root/new/g | 替换第1-10行所有的“root” |
| :%s/root/new/g | 替换文件内所有的“root” |
]# cp /etc/passwd /opt/s.txt
]# vim /opt/s.txt
• *开关参数的控制*
| *操作类型* | *设置指令* | *用 途* |
|---|---|---|
| 编辑器设置 | :set nu或nonu | 显示/不显示行号 |
| :set ai或noai | 启用/关闭自动缩进 |



