linux - file packaging and decompression

Before talking about the compression tools on Linux, it is necessary to understand the common and commonly used compressed package file formats. The most common ones on Windows are these three  *.zip, *.rar, *.7z suffixed compressed files. In addition to the above three common formats on Linux, there are also  *.gz, *.xz, *.bz2, *.tar, *.tar.gz, , *.tar.xz, *.tar.bz2which are briefly introduced as follows:

file extension illustrate
*.zip The zip program packs compressed files
*.rar rar program compressed files
*.7z 7zip program compressed files
*.tar tar program packaged, uncompressed files
*.gz Files compressed by the gzip program (GNU zip)
*.xz Files compressed by the xz program
*.bz2 Files compressed by the bzip2 program
*.tar.gz tar-packed, files compressed by the gzip program
*.tar.xz tar package, the file compressed by the xz program
*tar.bz2 tar-packed, files compressed by the bzip2 program
*.tar.7z tar package, 7z program compressed file

We have talked about so many compressed files and so many commands, but we generally only need to master a few commands, including  zip, rar, tar. These commands and the corresponding decompression commands will be introduced in turn.

 

 3.1 zip compression packager

  • Pack the folder with zip:
$ cd /home/shiyanlou
$ zip -r -q -o shiyanlou.zip /home/shiyanlou/Desktop $ du -h shiyanlou.zip $ file shiyanlou.zip 

The above command packages the directory /home/shiyanlou/Desktop into a file, and checks the size and type of the packaged file. In the first line of the command, the -r parameter indicates that the entire contents of the subdirectory are recursively packaged, and the -q parameter indicates the quiet mode, that is, no information is output to the screen. -o, indicates the output file, which needs to be followed by the package output file name. Use the  du command later to view the size of the packaged file (the command will be explained in detail later).

  • Set the compression level to 9 and 1 (9 is maximum, 1 is minimum), repack:
$ zip -r -9 -q -o shiyanlou_9.zip /home/shiyanlou/Desktop -x ~/*.zip
$ zip -r -1 -q -o shiyanlou_1.zip /home/shiyanlou/Desktop -x ~/*.zip 

A parameter is added here to set the compression level  -[1-9], 1 means the fastest compression but larger volume, 9 means the smallest volume but takes the longest. The last  -x one is to exclude the zip file we created last time, otherwise it will be packaged into the compressed file this time. Note: only absolute paths can be used here, otherwise it will not work.

We then use  du the command to view the default compression level, the minimum, the maximum compression level and the size of the uncompressed file:

$ du -h -d 0 *.zip ~ | sort

According to the man manual:

  • h, --human-readable (as the name suggests, you can try without it)
  • d, --max-depth (depth of the file being viewed)

At a glance, you can see that the default compression level should be the highest, and the effect is obvious, but the compressed file size you see after operating in the environment may be slightly different from the one shown in the figure, because the system will generate at any time during use. Some cache files are in the current user's home directory, which is irrelevant for us to learn the use of commands, and these differences can be ignored.

  • Create encrypted zip package

-e An encrypted archive can be created with  parameters:

$ zip -r -e -o shiyanlou_encryption.zip /home/shiyanlou/Desktop

Note: Regarding the  zip command, because of some compatibility issues between Windows system and Linux/Unix in text file format, such as newline (invisible character), in Windows it is CR+LF (Carriage-Return+Line-Feed: carriage return plus line feed) and LF (line feed) on Linux/Unix, so text edited on Linux may appear unwrapped when opened on Windows without processing. If you want the zip archive you created on Linux to decompress without any problems on Windows, then you also need to make some modifications to the command:

$ zip -r -l -o shiyanlou.zip /home/shiyanlou/Desktop

Need to add  -l parameters will be  LF converted  CR+LF to achieve the above purpose.

 

 3.2 Unzip the zip file using the unzip command

will  shiyanlou.zip extract to the current directory:

$ unzip shiyanlou.zip

To use quiet mode, extract the file to the specified directory:

$ unzip -q shiyanlou.zip -d ziptest

The directory specified above does not exist and will be created automatically. If you don't want to decompress and just want to see the contents of the archive you can use the  -l parameter:

$ unzip -l shiyanlou.zip

Note: We should also pay attention to compatibility issues when using unzip to decompress files, but here we are no longer concerned about the above issues, but the Chinese encoding issues. Usually, the compressed files created on Windows systems, if there are Chinese documents or The file with Chinese as the file name will use GBK or other encoding by default, while Linux uses the UTF-8 encoding by default. help you to deal with it), in order to solve this problem, we can specify the encoding type when decompressing.

Use the  -O(English letter, uppercase o) parameter to specify the encoding type:

unzip -O GBK 中文压缩文件.zip



 3.3 rar package compression command

rar It is also a commonly used compressed file format on Windows. On Linux, you can use  rar and  unrar tools to create and decompress rar archives respectively.

  • Installation  rar and  unrar tools:
$ sudo apt-get update
$ sudo apt-get install rar unrar
  • Create a tarball from the specified file or directory or add files to the tarball:
$ cd Desktop
$ rm *.rar $ rar a shiyanlou.rar . 

The above command uses  a parameters to add the current directory  . to an archive, which will be created automatically if it does not exist.

Note: There is no command parameter for rar  -, if it is added, an error will be reported.

  • Delete a file from the specified archive:
$ rar d shiyanlou.rar gvim.desktop
  • View the unpacked files:
$ rar l shiyanlou.rar
  • Use  unrar unzip  rar file

Unzip the full path:

$ unrar x shiyanlou.rar

Remove the path to decompress:

$ mkdir tmp
$ unrar e shiyanlou.rar tmp/

The rar command has a lot of parameters, and only some basic operations are covered above.

Tools are more commonly used on Linux  tar . tar was originally just a packaging tool, but it also supported tools such as 7z, gzip, xz, and bzip2. tar file) compression, there is no package compression of the file, so we don't need to learn other tools separately. The decompression and compression of tar are the same command, as long as the parameters are different, it is more convenient to use.

Let's first master  tar some basic usage of commands, that is, without compression, just packing (creating archive files) and unpacking.

  • Create a tarball:
$ tar -cf shiyanlou.tar /home/shyanlou/Desktop

In the above command, it -c means to create a tar package file, which -f is used to specify the file name to be created. Note that the file name must follow the  -f parameter. For example, it cannot be written  tar -fc shiyanlou.tar, but can be written  tar -f shiyanlou.tar -c ~. You can also add  -v parameters to output the packaged files visually. The above will automatically remove the absolute path  /, you can also use the  -P reserved absolute path character.

  • Unpack a file ( -x parameter) to an existing directory ( -C parameter) at the specified path:
$ mkdir tardir
$ tar -xf shiyanlou.tar -C tardir
  • Only view the unpacked file  -t parameters:
$ tar -tf shiyanlou.tar
  • Preserve file attributes and follow links (symlinks or soft links), sometimes we use tar to backup files when you restore on another host and want to preserve the attributes ( -p parameters) of the file and the source file the backup link points to instead of the link itself ( -h parameters) :
$ tar -cphf etc.tar /etc

For creating files in different compression formats, it is quite simple for tar. All you need is to change a parameter. Here we take the use of  gzip tools to create  *.tar.gz files as an example to illustrate.

  • -z We only need to add parameters  on the basis of creating the tar file  , and use gzip to compress the file:
$ tar -czf shiyanlou.tar.gz /home/shiyanlou/Desktop
  • Unzip the  *.tar.gz file:
$ tar -xzf shiyanlou.tar.gz

Now we only need to change one parameter to create or decompress the corresponding files using other compression tools:

Compressed file format parameter
*.tar.gz -z
*.tar.xz -J
*tar.bz2 -j

 

Having said so much, in fact, the parameters that are usually used are not that complicated, and you only need to remember the commonly used combinations. Common commands:

  • zip:
    • Packaging: zip something.zip something (please add the -r parameter to the directory)
    • Unpack: unzip something.zip
    • Specify the path: -d parameter
  • tar :
    • 打包:tar -zcvf something.tar something
    • 解包:tar -zxvf something.tar
    • Specify the path: -C parameter

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325127123&siteId=291194637