更新时间:2022-09-15 09:16:27
1、linux下压缩、解压以.gz结尾文件
压缩文件
命令格式:gzip [参数] [文件或者目录]
命令功能:压缩文件压缩后的文件名以".gz"为扩展名
命令参数:
-c或--stdout或--to-stdout 把压缩后的文件输出到标准输出设备不去更动原始文件。即保留源文件
-l或--list 列出压缩文件的相关信息。
-n或--no-name 压缩文件时不保存原来的文件名称及时间戳记。
-N或--name 压缩文件时保存原来的文件名称及时间戳记。
-q或--quiet 不显示警告信息。
-t或--test 测试压缩文件是否正确无误。
-r或--recursive 递归处理将指定目录下的所有文件及子目录一并处理。
-v或--verbose 显示指令执行过程。
-V或--version 显示版本信息。
使用实例:
1
2
3
4
5
6
7
8
9
|
[root@localhost case ] # ls
gzip gzip1 gzip2
[root@localhost case ] # gzip gzip
gzip1[root@localhost case ] # ls
gzip1 gzip2 gzip .gz
[root@localhost case ] # gzip -c gzip1 > gzio1.gz
[root@localhost case ] # ls
gzio1.gz gzip1 gzip2 gzip .gz
[root@localhost case ] #
|
解压文件
命令格式:gunzip [参数] [文件或者目录]
命令功能:解压文件名以".gz"为扩展名的文件
命令参数:
-c或--stdout或--to-stdout把解压后的文件输出到标准输出设备。
-l或--list列出压缩文件的相关信息。
-t或--test测试压缩文件是否正确无误。
使用实例:
1
2
3
4
5
6
7
|
[root@localhost case ] # ls
gzio1.gz gzip .gz
[root@localhost case ] # gunzip gzio1.gz
[root@localhost case ] # gunzip -c gzip.gz > gzip
[root@localhost case ] # ls
gzio1 gzip gzip .gz
[root@localhost case ] #
|
2、linux下压缩、解压以.zip结尾文件
压缩文件
命令格式:zip [参数] [压缩后的文件名] [压缩文件的路径]
命令功能:压缩文件压缩后的文件名以".zip"为扩展名
命令参数:
-b<工作目录> 指定暂时存放文件的目录
-d 从压缩文件内删除指定的文件。
-D 压缩文件内不建立目录名称。
-F 尝试修复已损坏的压缩文件。
-m 将文件压缩并加入压缩文件后删除原始文件即把文件移到压缩文件中。
-r 递归处理将指定目录下的所有文件和子目录一并处理。
-q 安静模式在压缩的时候不显示指令的执行过程
使用实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@localhost case ] # touch zip1 zip2
[root@localhost case ] # mkdir zipdir
[root@localhost case ] # ls
zip1 zip2 zipdir [root@localhost case ] # zip zip1.zip zip1
adding: zip1 (stored 0%)
[root@localhost case ] # ls
zip1 zip1.zip zip2 zipdir [root@localhost case ] # zip -r zip.zip zip2 zipdir/
adding: zip2 (stored 0%)
adding: zipdir/ (stored 0%)
[root@localhost case ] # ls
zip1 zip1.zip zip2 zipdir zip.zip [root@localhost case ] #
|
解压文件
命令格式:unzip [参数] [文件]
命令功能:解压文件名以".zip"为扩展名的文件
命令参数:
-l 显示压缩文件内所包含的文件
-C 压缩文件中的文件名称区分大小写。
-n 解压缩时不要覆盖原有的文件。
-o 不必先询问用户unzip执行后覆盖原有文件。
使用实例:
1
2
3
4
5
6
7
8
9
|
[root@localhost case ] # ls
zip1.zip zip.zip [root@localhost case ] # unzip zip.zip
Archive: zip.zip extracting: zip2
creating: zipdir/
[root@localhost case ] # ls
zip1.zip zip2 zipdir zip.zip [root@localhost case ] #
|
3、linux下压缩、解压以.bz2结尾文件
压缩文件
命令格式:bzip2 [参数] [文件]
命令功能:bzip2压缩完文件后会产生.bz2的压缩文件并删除原始的文件。
命令参数:
-k或--keep bzip2在压缩或解压缩后会删除原始的文件。若要保留原始文件请使用此参数。
-f或--force bzip2在压缩或解压缩时若输出文件与现有文件同名预设不会覆盖现有文件。若要覆盖请使用此参数。
-t或--test 测试.bz2压缩文件的完整性。
-z或--compress 强制执行压缩。
-d或--decompress 执行解压缩。
-压缩等级 压缩时的区块大小与 gzip 同样的都是在计算压缩比的参数 -9 *** -1 最快
-c或--stdout 将压缩与解压缩的结果送到标准输出。
使用实例:
1
2
3
4
5
6
7
8
9
|
[root@localhost case ] # ls
bzip1 bzip2
[root@localhost case ] # bzip2 bzip1
[root@localhost case ] # ls
bzip1.bz2 bzip2
[root@localhost case ] # bzip2 -k bzip2
[root@localhost case ] # ls
bzip1.bz2 bzip2 bzip2 .bz2
[root@localhost case ] #
|
解压文件
命令格式:bunzip2 [参数] [文件]
命令功能:解压文件名以".bz2"为扩展名的文件
命令参数:
-f或--force 解压缩时若输出的文件与现有文件同名时预设不会覆盖现有的文件。若要覆盖请使用此参数。
-k或--keep 在解压缩后预设会删除原来的压缩文件。若要保留压缩文件请使用此参数。
-s或--small 降低程序执行时内存的使用量。
-v或--verbose 解压缩文件时显示详细的信息。
-l,--license,-V或--version 显示版本信息。
使用实例:
1
2
3
4
5
6
7
8
9
|
[root@localhost case ] # ls
bzip1.bz2 bzip2 .bz2
[root@localhost case ] # bunzip2 bzip1.bz2
[root@localhost case ] # ls
bzip1 bzip2 .bz2
[root@localhost case ] # bunzip2 -k bzip2.bz2
[root@localhost case ] # ls
bzip1 bzip2 bzip2 .bz2
[root@localhost case ] #
|
4、linux下压缩、解压以.tar.*结尾文件
.tar.*结尾的文件是先用tar打包,再调用相应的压缩程序进行压缩,linux中常见的有两种,*.tar.gz和*.tar.bz2就属于这种先打包再压缩的文件
打包命令tar
命令格式:tar [参数] [文件、目录列表]
命令功能:对文件、目录进行打包备份
命令参数:
-c :建立一个新的压缩文件(create 的意思)
-x :解开一个压缩文件
-C [目录] :解压文件到某一指定目录
-t :查看 tarfile 里面的文件,特别注意,在参数的下达中, c/x/t 仅能存在一个!不可同时存在,因为不可能同时压缩与解压缩。
-z :是否同时具有 gzip 的属性?即调用gzip进行压缩
-j :是否同时具有 bzip2 的属性?即调用bzip2进行压缩
-v :压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!
-f :使用档名,请留意,在 f 之后要立即接档名!不要再加参数!
例如使用『 tar -zcvfP tfile sfile』就是错误的写法,要写成
『 tar -zcvPf tfile sfile』才对喔!
-p :使用原文件的原来属性(属性不会依据使用者而变)
-P :可以使用绝对路径来压缩!
-N :比后面接的日期(yyyy/mm/dd)还要新的才会被打包进新建的文件中!
--exclude FILE:在压缩的过程中,不要将 FILE 打包!多个文件写多个exclude,***用相对路径
文件路径要写成--exclude=queryweb/logs,不能写成--exclude=queryweb/logs/格式,不然无法排除log目录
--remove-files 压缩后删除源文件
1
|
[root@localhost remote] # tar -zcvf 2016-12-29.tar.gz *.log --remove-files
|
使用实例:
打包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@localhost case ] # touch file1 file2
[root@localhost case ] # mkdir dir1 dir2
[root@localhost case ] # cp file1 file2 dir1
[root@localhost case ] # cp file2 dir2
[root@localhost case ] # ls
dir1 dir2 file1 file2 [root@localhost case ] # tar -cvf file1.tar file1
file1 [root@localhost case ] # ls
dir1 dir2 file1 file1. tar file2
[root@localhost case ] # tar -cvf dir1.tar dir1 dir2
dir1/ dir1 /file2
dir1 /file1
dir2/ dir2 /file2
[root@localhost case ] # ls
dir1 dir1. tar dir2 file1 file1. tar file2
[root@localhost case ] #
|
查看包内包含的文件
1
2
3
4
5
6
7
|
[root@localhost case ] # tar -tvf dir1.tar
drwxr-xr-x root /root 0 2014-09-06 05:45 dir1/
-rw-r--r-- root /root 0 2014-09-06 05:45 dir1 /file2
-rw-r--r-- root /root 0 2014-09-06 05:45 dir1 /file1
drwxr-xr-x root /root 0 2014-09-06 05:45 dir2/
-rw-r--r-- root /root 0 2014-09-06 05:45 dir2 /file2
[root@localhost case ] #
|
打包时排除某一文件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@localhost case ] # tar --exclude=file1 -cvf dirfile2.tar dir1
dir1/ dir1 /file2
[root@localhost case ] # tar -tvf dirfile2.tar
drwxr-xr-x root /root 0 2014-09-06 05:45 dir1/
-rw-r--r-- root /root 0 2014-09-06 05:45 dir1 /file2
[root@localhost case ] # ll dir1
total 0 -rw-r--r-- 1 root root 0 Sep 6 05:45 file1 -rw-r--r-- 1 root root 0 Sep 6 05:45 file2 [root@localhost ~] # tar cvf ProxyServer1.tar.gz ProxyServer --exclude=ProxyServer/lib
[root@localhost ~] # tar cvf ProxyServer2.tar.gz ProxyServer --exclude=ProxyServer/derby.log
[root@localhost ~] #
|
解包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@localhost case ] # ls
dir1. tar dirfile2. tar file1. tar
[root@localhost case ] # tar -xvf dir1.tar
dir1/ dir1 /file2
dir1 /file1
dir2/ dir2 /file2
[root@localhost case ] # ls
dir1 dir1. tar dir2 dirfile2. tar file1. tar
[root@localhost case ] # mkdir dir3
[root@localhost case ] # tar -xvf dirfile2.tar -C dir3/
dir1/ dir1 /file2
[root@localhost case ] # ll dir3/
total 4 drwxr-xr-x 2 root root 4096 Sep 6 05:45 dir1 [root@localhost case ] #
|
打包、压缩成.tar.gz结尾的文件
1
2
3
4
5
6
7
|
[root@localhost case ] # ls
file [root@localhost case ] # tar -zcvf file.tar.gz file
file [root@localhost case ] # ls
file file . tar .gz
[root@localhost case ] #
|
解压.tar.gz结尾的文件
1
2
3
4
5
6
7
|
[root@localhost case ] # ls
file . tar .gz
[root@localhost case ] # tar -zxvf file.tar.gz
file [root@localhost case ] # ls
file file . tar .gz
[root@localhost case ] #
|
解压tar包中某个文件
1
|
[root@localhost src] # tar zxvpf queryweb20170327.tar.gz queryweb/webapps/commodity
|
这里的queryweb/webapps/commodity这个路径是压缩包里的路径,可以通过tar -tf 来列出来的路径,前面不要再加一个“/”,否则就报错了
打包、压缩成.tar.bz2结尾的文件
1
2
3
4
5
6
7
|
[root@localhost case ] # ls
file [root@localhost case ] # tar -jcvf file.tar.bz2 file
file [root@localhost case ] # ls
file file . tar .bz2
[root@localhost case ] #
|
解压.tar.bz2结尾的文件
1
2
3
4
5
6
7
|
[root@localhost case ] # ls
file . tar .bz2
[root@localhost case ] # tar -jxvf file.tar.bz2
file [root@localhost case ] # ls
file file . tar .bz2
[root@localhost case ] #
|
查看文件的大小
1
2
3
|
[root@localhost case ] # du -h file.tar.bz2
4.0K file . tar .bz2
[root@localhost case ] #
|
本文转自 justin_peng 51CTO博客,原文链接:http://blog.51cto.com/ityunwei2017/1316788,如需转载请自行联系原作者