Linux—文件的压缩与解压缩
1.将/etc/passwd文件中ntp用户的信息写入root用户家目录下的file1文件中,将sshd用户的信息写入root用户家目录下的file2文件中。
方法一:
[[email protected] ~]# vim /etc/passwd
[[email protected] ~]# touch file1
[[email protected] ~]# vim file1
[[email protected] ~]# cat file1
ntp:x:38:38::/etc/ntp:/sbin/nologin
[[email protected] ~]# vim /etc/passwd
[[email protected] ~]# touch file2
[[email protected] ~]# vim file2
[[email protected] ~]# cat file2
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[[email protected] ~]#
输入命令: yy 复制光标所在行
输入命令: p 粘贴到光标下一行
方法二:
[[email protected] ~]# grep ntp /etc/passwd > file1
[[email protected] ~]# grep sshd /etc/passwd > file2
[[email protected] ~]# cat file1
ntp:x:38:38::/etc/ntp:/sbin/nologin
[[email protected] ~]# cat file2
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[[email protected] ~]#
文本过滤 : grep可以根据指定的字符串,对文件的每一行进行搜索,如果找到了这个字符串,就输出该行的内容
2.将file1和file2文件打包并以bzip2的方式压缩为ff.tar.bz2。
[[email protected] ~]# tar jcvf ff.tar.bz2 file1 file2
file1
file2
[[email protected] ~]# ll
文件的归档即打包文件 : tar [主选项+辅选项] 文件或目录
主选项 : 只能出现一个主选项
c— create 创建一个新归档文件
x— 从归档文件中提取文件出来
t— 列出归档文件的内容,查看已经打包了哪些文件,重点在查看文件名
辅选项 :可以出现多个辅选项
z—通过gzip的支持进行压缩/解压缩,一般格式为*.tar.gz
j— 通过bzip2的支持进行压缩/解压缩,一般格式为*.tar.bz2
v— 归档或解包过程中显示被打包的文件
C—这个参数用在解压缩时,若要在特定目录解压缩,可以使用这个参数
f— 输出结果到文件,必须写该选项
3.保留ff.tar.bz2文件并将其解压到redhat用户的家目录下。
[[email protected] ~]# tar xvf ff.tar.bz2 -C /home/redhat
file1
file2
[[email protected] ~]# ll /home/redhat
total 8
drwxr-xr-x. 2 redhat redhat 6 Mar 5 21:46 Desktop
drwxr-xr-x. 2 redhat redhat 6 Mar 5 21:46 Documents
drwxr-xr-x. 2 redhat redhat 6 Mar 5 21:46 Downloads
-rw-r--r--. 1 root root 36 Mar 18 23:11 file1 //解压成功
-rw-r--r--. 1 root root 67 Mar 18 23:11 file2
drwxr-xr-x. 2 redhat redhat 6 Mar 5 21:46 Music
drwxr-xr-x. 2 redhat redhat 6 Mar 5 21:46 Pictures
drwxr-xr-x. 2 redhat redhat 6 Mar 5 21:46 Public
drwxr-xr-x. 2 redhat redhat 6 Mar 5 21:46 Templates
drwxr-xr-x. 2 redhat redhat 6 Mar 5 21:46 Videos
[[email protected] ~]#
解压 : tar xvf 文件名 该命令会自动判断归档文件的压缩格式,自动调用相关程序进行解压缩
解压到指定路径 : tar xvf 文件名 -C 目标路径
例如· : tar -xjv -f ff.tar.bz2 -C 欲解压缩的目录