Compressing and decompressing files under Linux is quite different from the Windows environment. Under Windows, you only need to install a tool like Winrar to decompress most files, while under Linux command line there are different compression and decompression methods for each kind of file.
Common Linux compression and decompression commands
Using tar to package files
The most common packaging program under Linux is tar (note that tar is packaging, not compression!) We often refer to the package that is created using the tar program as a tarball, and the commands for tarball files usually end with .tar. After generating a tarball, you can use other programs to compress it. tar command has a lot of options (you can check them with man tar).
Format: tar [primary option + secondary option] file or directory
Primary options: they cannot be present at the same time, telling tar what to do is mandatory
- -c: create a new zip file, equivalent to packing
- -x: release files from zip file, equivalent to unpacking
- -t: list the contents of the zip file
Secondary options: Optional
- -C: switch to the specified directory
- -z: Does it also have the properties of gzip? That is, does it need to be compressed or decompressed with gzip? The usual format is gz or xx.tgz
- -j: Does it also have the properties of bzip2? i.e. does it need to be compressed or decompressed with bzip2? Usually the format is bz2
- -v: show files during compression
- -f: Specify the compressed file, note that the file name should be immediately after f (your own), no other parameters should be added.
- -p: use the original attributes of the original file
- -exclude FILE: don’t pack the FILE during the compression process
Common commands.
Compress.
- tar -cvf file or directory name Packed object - pack only, not compress
- tar -zcvf file or directory name - packed, compressed with gzip
- tar -jcvf file or directory name - packed, compressed with bzip2
Check: (Note: choose whether to add z depending on whether it is gzip compressed or not)
- tar -ztvf file or directory name – check for those files in the compressed file
Decompress.
- tar -xvf file or directory name
- tar -zxvf file or directory name
Example.
- tar -cvf file2.tar /home/user2/file2 #pack the /home/user2/file2 file into a tar in the current directory, it should be noted that: the source file identified with an absolute path, after compressed with the tar command, the file name along with the absolute path (home/user2/, the root directory “/” is automatically removed) is compressed in together with the home file, two is not file2 after decompression using tar.
- tar -cvf file2.tar -C /home/user2/file2 # Use the -C command to change the working directory of tar from the current directory to /home/user2 to compress the file2 file (without absolute path) into tar. Note: -C dir is used to change the working directory and is valid until the next -C dir in that command.
- tar -xvf file2.tar -C /home/user2/file2 # Decompress the file to the specified directory path.
In addition to the most basic packing function, tar can also call other compression software, such as gzip, bzip2, etc., while packing or unpacking.
tar calls gzip
gzip is a compression program developed by the GNU organization. Files that end in .gz are the result of gzip compression. The opposite of gzip is the decompression program gunzip.
- Decompression 1: gunzip FileName.gz
- Decompress 2: gzip -d FileName.gz
- Compression: gzip FileName
The -z argument is used in tar to invoke gzip.
- tar -czf all.tar.gz *.jpg # This command is to make a tarball of all .jpg files and compress them with gzip to produce a gzip compressed package with the name gz
- tar -xzf all.tar.gz #This command unpacks the above generated package.
Also, if you encounter a .tgz file, the operation is the same. .tgz is short for .tar.gz.
tar calls bzip2
bzip2 is a much more powerful compression program. Files ending in .bz2 are the result of bzip2 compression. The opposite decompression program to bzip2 is bunzip2.
- Decompress 1: bzip2 -d FileName.bz2
- Unzip2: bunzip2 FileName.bz2
- Compression: bzip2 -z FileName
The -j parameter is used in tar to invoke gzip.
- tar -cjf all.tar.bz2 *.jpg # This command is to make a tarball of all .jpg files and compress them with bzip2 to produce a bzip2 compressed package with the package name bz2
- tar -xjf all.tar.bz2 #This command unpacks the above generated package.
The very uncommon .bz file is also decompressed using the same approach.
tar calls compress
compress is also a compression program. Currently, not as many people use compress as gzip and bzip2. Z-terminated files are the result of bzip2 compression. The opposite of compress is uncompress.
- uncompress: uncompress FileName.
- Compress: compress FileName
The -Z parameter is used in tar to invoke compress. here’s an example.
tar -cZf all.tar.Z *.jpg # This command will compress all .jpg files into a tarball and compress them to create an uncompressed package called all.tar.Z
tar -xZf all.tar.Z #This command will uncompress the above generated package
zip and unzip in Linux
zip command explained
zip is a widely used compression program that compresses files to produce a separate zip file with a “.zip” extension.
|
|
Parameters.
- -A Adjusts the executable auto-decompression file.
- -b
<working directory>
Specifies the directory to temporarily store the files. - -c Annotate each compressed file.
- -d Delete the specified files from the compressed file.
- -D Does not create directory names within the compressed file.
- -f This parameter has a similar effect to specifying the “-u” parameter, but not only updates existing files, but also adds files to the zip file if they did not exist in the zip file.
- -F Attempts to repair a corrupted zip file.
- -g Append files to an existing zip file instead of creating a new one.
- -h Online help.
- -i
<paradigm style>
Compress only files that match the conditions. - -j Save only the file name and its contents without storing any directory names.
- -J Remove unnecessary data in front of compressed files.
- -k Use MS-DOS compatible format for file names.
- -l Replace LF characters with LF+CR characters when compressing files.
- -ll Replace LF+CR characters with LF characters when compressing files.
- -L Display copyright information.
- -m Delete the original file after compressing and adding it to the zip file, i.e., move the file to the zip file.
- -n
<word-terminated string>
Does not compress files with a specific word-terminated string. - -o Set the change time of the compressed file to the same as the file that has the latest change time within the compressed file.
- -q Does not display the command execution process.
- -r Recursively processes all files and subdirectories in the specified directory together.
- -S Contains system and hidden files.
- -t
<date time>
Sets the date of the compressed file to the specified date. - -T Checks that each file within the backup file is correct.
- -u Replaces the newer files inside the zip file.
- -v Display command execution process or display version information.
- -V Saves the file properties of the VMS operating system.
- -w Suppose the version number in the file name, this parameter is only valid under VMS operating system.
- -x
<paradigm style>
Exclude eligible files when compressing. - -X Does not save additional file attributes.
- -y Saves the symbolic link directly, not the file to which the link refers.
- -z Add comments to compressed files.
- -$ Saves the volume name of the disk where the first compressed file is located.
- -
<compression efficiency>
The compression efficiency is a value between 1 and 9.
unzip command detailed explanation
unzip is the unzip procedure for .zip compressed files.
|
|
Parameters.
- -c Display the result of unzipping to the screen, and do the appropriate conversion of characters.
- -f Update existing files.
- -l Displays the files contained within the compressed file.
- -p Similar to the -c argument, displays the result of decompression to the screen, but does not perform any conversions.
- -t Check if the compressed file is correct.
- -u is similar to the -f argument, but in addition to updating the existing files, it will also decompress other files in the zip file to the directory.
- -v Displays detailed information when executed yes.
- -z Displays only the comment text of the compressed file.
- -a Perform the necessary character conversions on text files.
- -b Do not perform character conversions on text files.
- -C Case-sensitive file names in compressed files.
- -j Do not process the original directory path in the compressed file.
- -L Change all filenames in compressed files to lowercase.
- -M Send the output to the more program for processing.
- -n Do not overwrite the original files when decompressing.
- -o No need to ask the user first, unzip overwrites the original file after execution.
- -P
<password>
Use zip’s password option. - -q Execute without displaying any information.
- -s Converts whitespace characters in filenames to underline characters.
- -V Preserve the file version information of VMS.
- -X Unzip while saving back the original UID/GID of the file.
- [.zip file] Specifies a .zip compressed file.
- [files] Specifies which files in the .zip archive to process.
- -d
<directory>
Specifies the directory where the files will be stored after decompression. - -x
<file>
Specifies which files in the .zip zip archive not to process. - -Z unzip -Z is equivalent to executing the zipinfo command
So many parameters are estimated to have seen dizzy, the actual process is not so troublesome, the following sample list.
- zip -r yasuo.zip abc.txt dir1 #compress a file txt and a directory dir1 into a zip
- unzip yasuo.zip # unzip the file to the current directory
- unzip abc? .zip # simultaneously unzip the files in the current directory zip, zip and abc3.zip? means a character, if you use * means any multiple characters
- unzip -v large.zip # only view the contents of the zip file, not decompression
- unzip -t large.zip # verify the integrity of the zip file
- unzip -j music.zip # Extract all the files in the zip archive to the first level directory, instead of creating multiple levels of directories with the same structure as the original one
Compressing and decompressing rar files under Linux
.rar is the most common compression file format in Windows, in Linux you need to install rarlinux if you want to use it, official: http://www.rarsoft.com/download.htm
Or use RPM to install UNRAR, download at: http://pkgs.repoforge.org/unrar/
The rar and unrar commands can be used after the installation is complete. Simple usage.
|
|
Note: The <command>
part of rar does not have a “-” sign, only the <option>
part has a “-” sign, so pay attention to this.
Example 1: Adding files or directories to a compressed archive, using the a command.
For example, add file1 to abc.rar, use a or m command, a command to add file1 to abc.rar file to keep the original file1 file unchanged, m command to move file1 file to file1.rar file (the original file1 file will be deleted after the compression is completed, note: m command only operates on files.)
|
|
Explanation: If abc.rar file does not exist at this time, it will create abc.rar text file by itself, if abc.rar file already exists, it will compress the file1 file into abc.rar file, if a file1 file already exists in abc.rar file, it will update the file1 file. And the original file1 file still exists in the current directory, if you want to move the file1 file into file1.rar please use the m command, you can do the same for the directory.
Note: If you enter only “rar a file1.rar” command, and no file name or directory name, then all the files and folders in the current directory will be compressed into the file1.rar file. This point should be noted.
Example 2: decompression of the contents of the abc.rar file
You can use the e or x command, assuming that the abc.rar directory has a file named file1 and a directory named test, test directory has a file named file2.
|
|
Note: Using the e command will extract the file1 file in abc.rar along with the file2 file in the test directory to the current directory. If you want to keep the directory structure in the abc.rar directory please use the x command.
|
|
Note: At this point, the file1 file and the test directory will be extracted to the current folder.
Example 3: Adding comments to the entire zip file
|
|
Description:After entering this command, the bottom of the screen will display
and there is a cursor blinking, from the location of the blinking cursor to enter the comment information, finished typing press Ctrl + D to end the input
Example 4:Adding comments to a single file in a zip file, using the cf command.
If you want to add a comment to file1 in the abc.rar file
|
|
At this point the bottom of the screen will show
|
|
Type the comment you want to add for file1 from the cursor blinking out, Ctrl+D to end the input
Example 5:Writing comments for the entire file to a single file, using the cw command.
Add the comment you want to write for abc.rar to the test.txt file
|
|
Description: If a file named test.txt does not exist in the current directory, it will create a file named test.txt by itself and write the comments of abc.rar to the text.txt file, if a file named text.txt already exists in the current directory, you will be prompted whether to overwrite the existing file, if you choose Yes or If you choose Yes or All, the original content of test.txt will be cleared and the comments of abc.rar will be written to text.txt file.
Example 6: To delete a file or directory in a compressed archive, use the d command .
For example, you want to delete the file1 file in the abc.rar archive.
|
|
Note: The command deletes the file1 file in the abc.rar file, which is also valid for the directory.
The above is the commonly used compression and decompression commands, <option>
part of the use is no longer introduced, please try it yourself.
The use of the unrar command.
The commands in unrar have the same effect as rar. You can see that unrar contains only some of the commands of rar, so you can do all the operations using rar.
7z file decompression on Linux
Personally, I prefer to install 7-zip on windows, and often use the .7z format when compressing. To decompress .7z files, you first need to install 7zip software.
|
|
The command to use 7zip is 7za.
How to use it after installation: 7za {a|d|l|e|u|x} zip file name {file list or directory, optional}
- a add files to the zip archive or create a zip archive, e.g. add jpg to 7z, execute: 7za a 001.7z 001.jpg; pack the 001 directory execute: 7za a 001.7z 001.
- d Remove files from the zip, such as removing jpg from 7z, execute: 7za d 001.7z 001.jpg
- l List the files in the zip, for example, list the files in 7z, execute: 7za l 001.7z
- e decompression to the current directory, the directory structure will be destroyed, such as the following directory and file 123/456/789.html in the rar
- Execute: 7za e 001.rar, the directory 123 and 456 and the file html will be stored in the current directory.
- x Extract with full path.
Example.
- 7za a vps12.7z /www # Pack the files in the www directory under the root directory
- 7za x vps12.7z -o/home/www # unpack, -o means output directory, note that there is no space between it and the directory path
Linux decompression Chinese messy code problem solution
In the above article, we learned the common Linux decompression commands. But in the actual Linux use process, decompression still has many doors and pits exist.
Graphical environment decompression software
- Xarchive: Manjaro (Deepin version) comes with its own, lightweight decompression software
- Peazip: supports both Windows and Linux systems
- Engrampa: comes with decompression software for MATE desktop environment
- B1 Free Archiver: supports both PC (Linux, Windows, Mac OS) and cell phone (Android)
The above is the decompression software I have installed, no matter which one is similar in function, the unified problem is that decompression of .rar and .zip files created under Windows will have Chinese messy code problem, because the default character set of Windows Chinese version is not UTF-8 but CP936.
Command line environment decompression software
In addition to the previously introduced tar, zip/unzip, rar/unrar, p7zip, here is a special recommendation for a command-line decompression software unar, unar from the Mac OS decompression software The Unarchiver, but to the Linux The power of Unar compared to other software is that it can set the encoding when decompressing.
unar common commands
- Unzip the archive: unar document.zip
- Various formats are supported by default, including zip, rar, etc.
- Specify where to keep the unzip result: unar document.zip -o /home/dir
- Specify the encoding: unar -e GBK document.zip
- Use CP936, GB2312, GBK, GB18030 are the same
- Specify the unzip password: unar -p password document.zip
- You can also specify no password, the command line interface will show an interactive prompt asking for a password