Home > php教程 > PHP开发 > Common Linux commands---tar | zip (decompression)

Common Linux commands---tar | zip (decompression)

高洛峰
Release: 2016-12-19 14:23:37
Original
1544 people have browsed it

Reducing file size has two obvious benefits. One is that it can reduce storage space, and the other is that when transferring files through the network, it can reduce the transfer time. gzip is a command commonly used in Linux systems to compress and decompress files. It is convenient and easy to use. Gzip can not only be used to compress large, rarely used files to save disk space, but can also be used together with the tar command to form a popular compressed file format in the Linux operating system. According to statistics, the gzip command has a compression rate of 60% to 70% for text files.

1. zip

zip -r myfile.zip ./*
Compress all files and folders in the current directory into the myfile.zip file, -r means recursively compress all files in the subdirectory.

zip -d myfile.zip smart.txt
Delete the smart.txt file in the compressed file


zip -m myfile.zip ./rpm_info.txt
Add the rpm_info.txt file to myfile.zip in the compressed file

zip -r filename.zip filesdir

In this example, filename.zip represents the file you created, and filesdir represents the directory where you want to place the new zip file. The -r option specifies that you want to recursively include all files contained in the filesdir directory.

       zip -r filename.zip file1 file2 file3 /usr/work/school

The above command compresses the contents of the file1, file2, file3, and /usr/work/school directories (assuming this directory exists). Then put it into the filename.zip file.

2. unzip

unzip -o -d /home/sunny myfile.zip
Extract the myfile.zip file to /home/sunny/
-o: Overwrite the file without prompting;
- d:-d /home/sunny indicates to decompress the file to the /home/sunny directory;

unzip abc?.zip

I have abc1.zip, abc2.zip and abc3.zip in the current directory. If you want to decompress them together,

? represents one character, and * represents any number of characters.

unzip -v large.zip

I have a large compressed file large.zip, I don’t want to decompress it, I just want to see what’s in it

unzip -t large.zip

View Check if the compressed file is downloaded correctly

unzip -j music.zip

I used the -v option and found that there are many directories and subdirectories in the music.zip compressed file, and the subdirectories are actually song mp3 files. I I want to download all these files to the first-level directory instead of building directories layer by layer

3. tar

We know that there are only two most common compressed files under Windows, one is zip , the other one is .rap. But Linux is different. It has many compressed file names such as .gz, .tar.gz, tgz, bz2, .Z, .tar, etc. In addition, .zip and .rar under Windows can also be used under Linux. tar itself does not have compression capabilities. It is implemented by calling the compression function. The necessary parameters are as follows:

-A Add a compressed file to the existing compression

-B Set the block size

-c Create a new compressed file

-d Record file The difference

-r Add files to already compressed files

-u Add changed and existing files to existing compressed files

-x Extract files from compressed files

-t Show compressed files Content

-z Supports gzip decompressed files

-j Supports bzip2 decompressed files

-Z Supports compress decompressed files

-v Displays the operation process

-l File system boundary settings

-k Keeps the original files without overwriting

-m Keep the file from being overwritten

-W Confirm the correctness of the compressed file

Example 1: Pack all the files into a tar package

Command:

tar -cvf log.tar log2012.log

tar -zcvf log.tar.gz log2012.log

tar -jcvf log.tar.bz2 log2012.log
Copy after login

Output:

[root@localhost test]# ls -al log2012.log

---xrw-r-- 1 root root 302108 11-13 06:03 log2012.log

[root@localhost test]# tar -cvf log.tar log2012.log 

log2012.log

[root@localhost test]# tar -zcvf log.tar.gz log2012.log

log2012.log

[root@localhost test]# tar -jcvf log.tar.bz2 log2012.log 

log2012.log

[root@localhost test]# ls -al *.tar*

-rw-r--r-- 1 root root 307200 11-29 17:54 log.tar

-rw-r--r-- 1 root root   1413 11-29 17:55 log.tar.bz2

-rw-r--r-- 1 root root   1413 11-29 17:54 log.tar.gz
Copy after login

Description:

tar -cvf log.tar log2012.log Only packaging, not compression!

tar -zcvf log.tar.gz log2012.log After packaging, compress it with gzip

tar -jcvf log.tar.bz2 log2012.log After packaging it, compress it with bzip2

The file name after the parameter f is itself We are accustomed to use .tar as identification. If the z parameter is added, .tar.gz or .tgz will be used to represent the gzip compressed tar package; if the j parameter is added, .tar.bz2 will be used as the tar package name.

Example 2: Check what files are in the above tar package

Command:

tar -ztvf log.tar.gz

输出:

[root@localhost test]# tar -ztvf log.tar.gz

---xrw-r-- root/root 302108 2012-11-13 06:03:25 log2012.log

说明:

由于我们使用 gzip 压缩的log.tar.gz,所以要查阅log.tar.gz包内的文件时,就得要加上 z 这个参数了。

实例3:将tar 包解压缩

命令:

tar -zxvf /opt/soft/test/log.tar.gz

输出:

[root@localhost test3]# ll

总计 0[root@localhost test3]# tar -zxvf /opt/soft/test/log.tar.gz

log2012.log

[root@localhost test3]# ls

log2012.log

[root@localhost test3]#

说明:

在预设的情况下,我们可以将压缩档在任何地方解开的

实例4:只将 /tar 内的 部分文件解压出来

命令:

tar -zxvf /opt/soft/test/log30.tar.gz log2013.log

输出:

[root@localhost test]# tar -zcvf log30.tar.gz log2012.log log2013.log 

log2012.log

log2013.log

[root@localhost test]# ls -al log30.tar.gz 

-rw-r--r-- 1 root root 1512 11-30 08:19 log30.tar.gz

[root@localhost test]# tar -zxvf log30.tar.gz log2013.log

log2013.log

[root@localhost test]# ll

-rw-r--r-- 1 root root   1512 11-30 08:19 log30.tar.gz

[root@localhost test]# cd test3

[root@localhost test3]# tar -zxvf /opt/soft/test/log30.tar.gz log2013.log

log2013.log

[root@localhost test3]# ll

总计 4

-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log

[root@localhost test3]#
Copy after login

说明:

我可以透过 tar -ztvf 来查阅 tar 包内的文件名称,如果单只要一个文件,就可以透过这个方式来解压部分文件!

实例5:文件备份下来,并且保存其权限

命令:

tar -zcvpf log31.tar.gz log2014.log log2015.log log2016.log

输出:

[root@localhost test]# ll

总计 0

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

-rw-r--r-- 1 root root      0 11-16 14:41 log2016.log

[root@localhost test]# tar -zcvpf log31.tar.gz log2014.log log2015.log log2016.log 

log2014.log

log2015.log

log2016.log

[root@localhost test]# cd test6

[root@localhost test6]# ll

[root@localhost test6]# tar -zxvpf /opt/soft/test/log31.tar.gz 

log2014.log

log2015.log

log2016.log

[root@localhost test6]# ll

总计 0

-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log

-rw-r--r-- 1 root root 0 11-16 14:41 log2016.log

[root@localhost test6]#
Copy after login

说明:

这个 -p 的属性是很重要的,尤其是当您要保留原本文件的属性时

实例6:在 文件夹当中,比某个日期新的文件才备份

命令:

tar -N "2012/11/13" -zcvf log17.tar.gz test

输出:

[root@localhost soft]# tar -N "2012/11/13" -zcvf log17.tar.gz test

tar: Treating date `2012/11/13' as 2012-11-13 00:00:00 + 0 nanoseconds

test/test/log31.tar.gz

test/log2014.log

test/linklog.log

test/log2015.log

test/log2013.log

test/log2012.log

test/log2017.log

test/log2016.log

test/log30.tar.gz

test/log.tar

test/log.tar.bz2

test/log.tar.gz
Copy after login

说明:

实例7:备份文件夹内容是排除部分文件

命令:

tar --exclude scf/service -zcvf scf.tar.gz scf/*

输出:

[root@localhost test]# tree scf

scf

|-- bin

|-- doc

|-- lib

`-- service

     `-- deploy

          |-- info

          `-- product

7 directories, 0 files

[root@localhost test]# tar --exclude scf/service -zcvf scf.tar.gz scf/* 

scf/bin/

scf/doc/

scf/lib/

[root@localhost test]#



更多Linux常用指令---tar | zip (解压缩)相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template