linux亦步亦趋(15)文件管理压缩与解压

在linux系统下我们也可以进行文件的压缩与解压,达到减少磁盘占用,提高管理密度的功能。

常用命令如下:

gzip命令:

该命令用于压缩文件,格式:gzip  文件;

  • 特点一:不支持压缩目录
  • 特点二:不保留源文件
  • 特点三:生成的格式为.gz
  • 例子如下:
[root@localhost test]# ls -l
总计 12
-rw-r--r-- 1 root root   32 07-23 22:20 test1.sh
drwxr-xr-x 2 root root 4096 07-24 12:30 testdir
-rwxrwxrwx 1 test test   32 07-23 22:19 test.sh
[root@localhost test]# gzip test.sh
[root@localhost test]# ls -l
总计 12
-rw-r--r-- 1 root root   32 07-23 22:20 test1.sh
drwxr-xr-x 2 root root 4096 07-24 12:30 testdir
-rwxrwxrwx 1 test test   54 07-23 22:19 test.sh.gz 
##ls的结果 :产生了一个.gz文件,同时源文件消失了  
[root@localhost test]#
[root@localhost test]# gzip testdir
gzip: testdir is a directory -- ignored
##对目录进行操作的时候发现是不支持的
[root@localhost test]#

 

gunzip命令:

该命令与gzip对应,是用来解压缩gzip产生的文件的。格式:gunzip 文件;

  • 与gzip相对应。
  • 与gzip -d 是一个意思。
  • 例子如下:
[root@localhost test]# ls
test1.sh  testdir  test.sh.gz
[root@localhost test]# gunzip test.sh.gz
[root@localhost test]# ls -l
总计 12
-rw-r--r-- 1 root root   32 07-23 22:20 test1.sh
drwxr-xr-x 2 root root 4096 07-24 12:30 testdir
-rwxrwxrwx 1 test test   32 07-23 22:19 test.sh
[root@localhost test]#
[root@localhost test]# ls -l
总计 12
-rw-r--r-- 1 root root   32 07-23 22:20 test1.sh
drwxr-xr-x 2 root root 4096 07-24 12:30 testdir
-rwxrwxrwx 1 test test   54 07-23 22:19 test.sh.gz
[root@localhost test]# gzip -d test.sh.gz
[root@localhost test]# ls -l
总计 12
-rw-r--r-- 1 root root   32 07-23 22:20 test1.sh
drwxr-xr-x 2 root root 4096 07-24 12:30 testdir
-rwxrwxrwx 1 test test   32 07-23 22:19 test.sh
[root@localhost test]#

 我们知道gzip不支持文件夹的压缩,这可咋整,不用纠结,我们可以使用tar命令。

tar命令:

该命令的用途是打包目录,即将一个目录打成一个文件。

  • 常用格式:tar -cvf 新文件名 目录名  打包一个目录
  • 常用格式:tar -xvf  tar文件 解压一tar文件
  • -c 表示创建文件
  • -v 显示过程详情
  • -f  指定文件名
  • -z 打包的同时进行压缩或解压缩
  • -x 解压缩

压缩的例子如下:

##只进行压缩,
[root@localhost test]# tar -cvf testdirdd testdir
testdir/
testdir/test.txt
[root@localhost test]# ls -l
总计 24
-rw-r--r-- 1 root root    32 07-23 22:20 test1.sh
drwxr-xr-x 2 root root  4096 07-24 12:30 testdir
-rw-r--r-- 1 root root 10240 07-24 13:20 testdirdd
-rwxrwxrwx 1 test test    32 07-23 22:19 test.sh
[root@localhost test]#
##生成的新文件:testdirdd
##同时进行压缩:
[root@localhost test]# tar -cvzf tetsdir.z testdir
testdir/
testdir/test.txt
[root@localhost test]# ls -l
总计 16
-rw-r--r-- 1 root root   32 07-23 22:20 test1.sh
drwxr-xr-x 2 root root 4096 07-24 12:30 testdir
-rwxrwxrwx 1 test test   32 07-23 22:19 test.sh
-rw-r--r-- 1 root root  181 07-24 13:28 tetsdir.z
[root@localhost test]#

解压缩的例子如下:

##解压后源文件还保留,生成的文件会把tar的后缀去掉
[root@localhost test]# tar -xvf testdir.tar
testdir/
testdir/test.txt
[root@localhost test]#
[root@localhost test]# ls -l
总计 24
-rw-r--r-- 1 root root    32 07-23 22:20 test1.sh
drwxr-xr-x 2 root root  4096 07-24 12:30 testdir  ##新生成的文件
-rw-r--r-- 1 root root 10240 07-24 13:38 testdir.tar ##源文件
-rwxrwxrwx 1 test test    32 07-23 22:19 test.sh
##如果是用-cvzf来压缩的 那么解压需要用xvzf来才行
 
[root@localhost test]# ls -l
总计 16
-rw-r--r-- 1 root root   32 07-23 22:20 test1.sh
drwxr-xr-x 2 root root 4096 07-24 12:30 testdir
-rw-r--r-- 1 root root  181 07-24 13:46 testdir.tar.gz
-rwxrwxrwx 1 test test   32 07-23 22:19 test.sh
[root@localhost test]#tar -xvzf testdir.tar.gz ##加上解压缩的参数选项-z
testdir/
testdir/test.txt
[root@localhost test]# ls -l
总计 16
-rw-r--r-- 1 root root   32 07-23 22:20 test1.sh
drwxr-xr-x 2 root root 4096 07-24 12:30 testdir
-rw-r--r-- 1 root root  181 07-24 13:46 testdir.tar.gz
-rwxrwxrwx 1 test test   32 07-23 22:19 test.sh
[root@localhost test]# ls -l ./testdir  ##证明解包和解压缩均成功
总计 4
-rw-r--r-- 1 root root 65 07-24 12:30 test.txt
在linux系统下我们也可以进行文件的压缩与解压,达到减少磁盘占用,提高管理密度的功能。 常用命令如下: 该命令用于压缩文件, 格式:gzip  文件;
  • 特点一:不支持压缩目录
  • 特点二:不保留源文件
  • 特点三:生成的格式为.gz
  • 例子如下:
[root@localhost test]# ls -l
总计 12
-rw-r--r-- 1 root root   32 07-23 22:20 test1.sh
drwxr-xr-x 2 root root 4096 07-24 12:30 testdir
-rwxrwxrwx 1 test test   32 07-23 22:19 test.sh
[root@localhost test]# gzip test.sh
[root@localhost test]# ls -l
总计 12
-rw-r--r-- 1 root root   32 07-23 22:20 test1.sh
drwxr-xr-x 2 root root 4096 07-24 12:30 testdir
-rwxrwxrwx 1 test test   54 07-23 22:19 test.sh.gz 
##ls的结果 :产生了一个.gz文件,同时源文件消失了  
[root@localhost test]#
[root@localhost test]# gzip testdir
gzip: testdir is a directory -- ignored
##对目录进行操作的时候发现是不支持的
[root@localhost test]#
  该命令与gzip对应,是用来解压缩gzip产生的文件的。格式:gunzip 文件;
  • 与gzip相对应。
  • 与gzip -d 是一个意思。
  • 例子如下:

猜你喜欢

转载自xuelianbobo.iteye.com/blog/2095922
今日推荐