归档及压缩
作用:1.将零散的数据进行归档 2.减少占用磁盘空间的大小
归档的含义
–将许多零散的文件整理为一个文件
–文件总的大小基本不变
压缩的含义
–按某种算法减小文件所占用空间的大小
–恢复时按对应的逆向算法解压
Linux常见的压缩格式
.gz gzip #压缩速度快
.bz2 bzip2
.xz xz #压缩比例最高
tar 集成备份工具
–-c:创建归档
–-x:释放归档
–-f:指定归档文件名称,必须放在所有选项的最后
–-z、-j、-J:调用 .gz、.bz2、.xz 格式的工具进行处理
–-t:显示归档中的文件清单
–-C:指定释放路径
tar 打包格式
tar 选项 /路径/压缩包名字 /路径/被归档压缩的数据…..
-c:创建归档 -f:指定压缩包名字
-z、-j、-J:调用 .gz、.bz2、.xz 格式的工具进行处理
[root@proxy ~]# tar -zcf /opt/nsd01.tar.gz /home /etc/passwd
tar: 从成员名中删除开头的“/”
[root@proxy ~]# ls /opt/
home nsd01.tar.gz redhat-release
[root@proxy ~]# tar -jcf /opt/file.tar.bz2 /home /etc/passwd
tar: 从成员名中删除开头的“/”
[root@proxy ~]# ls /opt/
file.tar.bz2 home nsd01.tar.gz redhat-release
[root@proxy ~]# tar -Jcf /opt/abc.tar.xz /home /etc/passwd
tar: 从成员名中删除开头的“/”
[root@proxy ~]# ls /opt/
abc.tar.xz file.tar.bz2 home nsd01.tar.gz redhat-release
–-t:显示归档中的文件清单
[root@localhost ~]# tar -tf /opt/nsd01.tar.gz
[root@localhost ~]# tar -tf /opt/file.tar.bz2
[root@localhost ~]# tar -tf /opt/abc.tar.xz
tar 解包格式
ar 选项 /路径/压缩包的名字 -C(大写) /路径/释放位置
]# mkdir /stu01 /stu02 /stu03
]# tar -xf /opt/nsd01.tar.gz -C /stu01/
]# ls /stu01/
]# ls /stu01/etc/
]# ls /stu01/home/
]# tar -xf /opt/file.tar.bz2 -C /stu02/
]# ls /stu02/
]# tar -xf /opt/abc.tar.xz -C /stu03/
]# ls /stu03/
案例1:创建一个备份包
使用 tar 工具完成以下备份任务:
–创建一个名为 /root/backup.tar.bz2 的归档文件
–其中包含 /usr/local 目录中的内容
–tar 归档必须使用 bzip2 进行压缩
]# tar -jcf /root/backup.tar.bz2 /usr/local/
tar: 从成员名中删除开头的“/”
]# ls /root/
]# tar -tf /root/backup.tar.bz2 #查看tar包内容
重定向与管道操作
重定向:将命令输出重新定向,写入文本文件中
>:覆盖重定向
>>:追加重定向
[root@localhost ~]# cat --help > /opt/test.txt
[root@localhost ~]# cat /opt/test.txt
[root@localhost ~]# hostname
[root@localhost ~]# hostname > /opt/test.txt
[root@localhost ~]# cat /opt/test.txt
[root@localhost ~]# ifconfig >> /opt/test.txt
[root@localhost ~]# less /opt/test.txt
[root@localhost ~]# echo 123456 > /opt/5.txt
[root@localhost ~]# cat /opt/5.txt
[root@localhost ~]# echo hello >> /opt/5.txt
[root@localhost ~]# cat /opt/5.txt
[root@localhost ~]# echo hahaixixi >> /opt/5.txt
[root@localhost ~]# cat /opt/5.txt
管道 | :将前面命令输出,传递给后面命令,作为后面命令的参数
显示/etc/passwd文件内容的8到12内容
]# head -12 /etc/passwd | tail -5
]# head -12 /etc/passwd | tail -5 | cat -n
]# cat -n /etc/passwd | head -12 | tail -5
显示/etc/passwd文件内容的第10行内容
]# head -10 /etc/passwd | tail -1
]# cat -n /etc/passwd | head -10 | tail -1
]# ifconfig | less
]# ifconfig | head -2
**find基本使用:
根据预设的条件递归查找对应的文件
find [查找目录] [条件]
常用条件表示:
-type 类型(f、d、l)
-name "文档名称"
-size +|-文件大小(k、M、G)
-user 用户名
-mtime 修改时间**
-type 类型(f文件、d目录、l快捷方式)
[root@localhost ~]# find /root -type f #查找是文件
[root@localhost ~]# find /root -type d #查找是目录
[root@localhost ~]# find /boot -type d
[root@localhost ~]# find /etc -type l
-name "文档名称"
]# find /etc/ -name "passwd"
]# find /etc/ -name "*tab"
]# find /etc/ -name "*.conf"
]# mkdir /root/nsd2007
]# touch /root/nsd05.txt
]# touch /root/nsd06.txt
]# find /root -name "nsd*"
/root/nsd02
/root/nsd05.txt
/root/nsd2007
/root/nsd06.txt
]# find /root -name "nsd*" -type d #以nsd开头必须是目录
/root/nsd02
/root/nsd2007
]# find /root -name "nsd*" -type f #以nsd开头必须是文件
/root/nsd05.txt
/root/nsd06.txt
-size +或-文件大小(k、M、G)
]# find /boot/ -size +300k
]# find /boot/ -size +10M
]# find /boot/ -size -1024M
-user 用户名(按照数据的所有者用户)
]# useradd student
]# find /home -user student
]# find / -user student #在整个系统中进行查找
/proc:不占用磁盘空间,反映内存的数据
]# find / -user student -type d
-mtime 按照修改时间 #都是过去时间
-mtime +10 #10天之前的数据
-mtime -10 #最近10天之内的数据
]# find /root/ -mtime -1
]# find /root/ -mtime +1000
find高级使用
处理查找的内容
–find [范围] [条件] -exec 处理命令 {} ;
–-exec:额外操作的开始,传递参数一次传递一个
–{}:代表find查询的每一个结果
–; : 额外操作的结束
]# rm -rf /opt/*
]# ls /opt/
]# find /boot/ -size +10M
]# find /boot/ -size +10M -exec cp {} /opt ;
]# ls /opt/
1.利用find查找所有用户 student 拥有的必须是文件,把它们拷贝到 /root/findfiles/ 文件夹:
]#useradd student
]#mkdir /root/findfiles
]#find / -user student -type f
]#find / -user student -type f -exec cp {} /root/findfiles/ \;
]#ls -A /root/findfiles/
vim编辑技巧
三个模式:命令模式、插入模式、末行模式
命令模式技巧:
[root@localhost ~]# cp /etc/passwd /opt/p.txt
[root@localhost ~]# vim /opt/p.txt
光标跳转
[root@localhost ~]# echo 123 > /opt/7.txt
[root@localhost ~]# echo abc > /opt/8.txt
[root@localhost ~]# vim /opt/8.txt
末行模式下 :r /opt/7.txt
末行模式下 :r /etc/passwd
[root@localhost ~]# cp /etc/passwd /opt/pass.txt
[root@localhost ~]# vim /opt/pass.txt
grep命令补充内容
]# grep root /etc/passwd
]# grep ^root /etc/passwd #必须以root开头的行
]# grep bash$ /etc/passwd #必须以bash结尾的行
^$:表示空行
]# cat /etc/default/useradd
]# grep ^$ /etc/default/useradd
]# grep -v ^$ /etc/default/useradd #去除空行
在Linux系统中,大多数配置文件内容,以#开头的行为注释行
显示/etc/login.defs配置有效配置(去除注释行,去除空行)
]# grep -v ^# /etc/login.defs
]# grep -v ^# /etc/login.defs | grep -v ^$
]# grep -v ^# /etc/login.defs | grep -v ^$ > /opt/12.txt
]# cat /opt/12.txt