Linux file compression and decompression commands

Source: Internet
Author: User
In Linux, we usually use the following file compression commands: bunzip2, bzip2, cpio, gunzip, gzip, and split (cut files ), zgrep (find matching regular expressions in compressed files), zip, unzip, tar, rar

In Linux, we usually use the following file compression commands: bunzip2, bzip2, cpio, gunzip, gzip, and split (cut files ), zgrep (find matching regular expressions in compressed files), zip, unzip, tar, and rar.
The last four are commonly used by me. I can write these four notes today.
1. tar:
Tar command: tar [option...] [FILE]...
Enter "tar -?" Get help information. the tar help document provides an example as follows:
Example
Tar-cf archive.tar foo bar # create archive.tar from file foo and bar.
Tar-tvf archive.tar # list all the files in archive.tar.
Tar-xf archive.tar # expand all the files in archive.tar.
If you want to compress the folder, enter:
Tar-cvf dirArchive.tar/dirName
Similarly, if you want to extract an archive file to a specific folder:
Tar-xvf dirArchive.tar/dirName
Next, let's review it. We first create a folder and place several folders and files in it:
First, create a tar file and compress a folder (testdir ):
Tar-cf testdir.tar testdir
Then list the structure of the compressed package:
Then we append the file to the compressed package:
Of course, the above one-step operation is unnecessary, and this file is already in the compressed package.
Then let's look at the structure of the compressed package:
If you use the-v option in the example, all the file attributes in the compressed package are listed. Including permissions, creator, byte size, modification time, and File name:
Extract from the tar compressed file and display the file list:
In general, the commonly used options include the primary operation options:
In addition, I want to practice some other usage:
1. when compressing the testdir Directory, exclude the test file in the root path of the folder:
2. only compression ratio of objects Updated at the specified time:
3. compress all files or folders with a prefix of "te" (or files with a suffix of "st ):
The following are some common options:
-P, -- preserve-permissions, -- same-permissions
Retain the permissions of the extracted files (by default, only the superuser service is used)
-- Preserve is the same as-p and-s.
-- Keep the owner relationship consistent when the same-owner tries to decompress the package (this option is used by the super user by default)
-R, -- append: append the file to the end of the archive.
-U, -- update: only append files that are updated compared with the replicas in the archive.
-K, -- keep-old-files do not replace existing files during decompression
-- Keep-newer-files
Do not replace an existing file that is newer than the archive copy.
-- No-overwrite-dir: retains the metadata of an existing directory.
-- Overwrite: overwrites the existing file during decompression.
-- Overwrite-dir
Overwrite the metadata of an existing directory during decompression (default)
-- Recursive-unlink clear the directory level before extracting the Directory
-- Remove-files: delete files after they are added to the archive.
-N, -- newer = DATE-OR-FILE, -- after-date = DATE-OR-FILE only saves files updated compared to DATE-OR-FILE.

2. rar:
Rar- - <@ Listfiles...>
The default package for Linux does not contain the rar compression tool. Therefore, we must first install the rar:
Sudo apt-get install rar
You can only enter one (and only one) from the given options. let's take a look at the list of commands it provides:
For example, we want to compress the file:
After compression, we can use the command l to obtain the specific structure information of the compressed file:
You can even use the command p to output all the information in the compressed file to the console:
Some provide some functional options.
For example, to update a compressed file in append mode, you can use-ad:
Or to exclude a file, you can use the switch-x (note that the string to be excluded must be followed by x without adding any symbols ), for example, we want to exclude all files starting with "te" in the root path of the folder:
There is also encryption, with the switch-p, the same, the password is followed by p:
If you decompress the file or open the file directly, you will be asked to enter the password:
In addition, some common options include:
Ag [format] Generate archive name using the current date
Cl Convert names to lower case
Cu Convert names to upper case
Df Delete files after archiving
Ed Do not add empty directories
Dw Wipe files after archiving
For other commands and switches, refer to the help manual.

3. zip and unzip:
Zip [-options] [-B path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
Unzip [-Z] [-opts [modifiers] file).zip] [list] [-x xlist] [-d exdir]
The use of zip and unzip is relatively simple, and even the help documentation is very simple, so there is no notes to write, just look at the Help documentation.
Linux zip command details
Function description: compress a file.
Syntax: zip [-AcdDfFghjJKlLmoqrSTuvVwXyz $] [-B <工作目录> ] [-Ll] [-n <字尾字符串> ] [-T <日期时间> ] [- <压缩效率> ] [Compressed file] [file...] [-I <范本样式> ] [-X <范本样式> ]
Note: zip is a widely used compression program. after files are compressed, other compressed files with the ". zip" extension will be generated.
Parameters:
-A: adjust the executable automatic decompression file.
-B <工作目录> Specifies the directory for storing files temporarily.
-C adds comments to each compressed file.
-D. delete the specified file from the compressed file.
-D. The directory name is not created in the compressed file.
-F this parameter works similarly to the specified "-u" parameter, but not only updates existing files, if some files do not exist in the compressed file, this parameter is used to add it to the compressed file.
-F tries to repair damaged compressed files.
-G compresses the file and attaches it to the existing compressed file, instead of creating a new compressed file.
-H online help.
-I <范本样式> Only files that meet the criteria are compressed.
-J only saves the file name and its content, without storing any directory name.
-J: delete unnecessary data before the compressed file.
-K uses a file name in MS-DOS-compatible format.
-L replace the LF character with the LF + CR character when compressing the file.
-Replace the LF + CR character with the LF character when compressing the file.
-L displays the copyright information.
-M: After compressing the file and adding it to the compressed file, delete the original file, that is, move the file to the compressed file.
-N <字尾字符串> Do not compress files with specific character strings.
-O the file with the latest change time in the compressed file is used as the standard, and the change time of the compressed file is set to the same as that of the file.
-Q: The command execution process is not displayed.
-R recursive processing: All files and subdirectories in the specified directory are processed together.
-S contains the system and hidden files.
-T <日期时间> Set the date of the compressed file to the specified date.
-T check whether each file in the backup file is correct.
-U replace the newer file into the compressed file.
-V displays the command execution process or version information.
-V stores the file attributes of the VMS operating system.
-W indicates the version number in the file name. this parameter is only valid in the VMS operating system.
-X <范本样式> Exclude files that meet the criteria during compression.
-X does not save additional file attributes.
-Y directly saves the symbolic connection, not the file pointed to by the connection. this parameter is only valid in UNIX and other systems.
-Z adds comments to the compressed file.
-$ Name of the volume on which the first compressed file is stored.
- <压缩效率> The compression efficiency is a value between 1 and 9.
Linux unzip command details
Function description: decompress the zip file.
Syntax: unzip [-cflptuvz] [-agCjLMnoqsVX] [-P <密码> [[.Zip file] [file] [-d <目录> ] [-X <文件> ] Or unzip [-Z]
Note: unzipis the decompression program for. ZIP compressed files.
Parameters:
-C displays the extracted results on the screen and converts the characters properly.
-F: update an existing file.
-L displays the files contained in the compressed file.
The-p parameter is similar to the-c parameter. the decompressed result is displayed on the screen, but no conversion is performed.
-T check whether the compressed file is correct.
The-u parameter is similar to the-f parameter, but in addition to updating existing files, other files in the compressed file are also extracted to the directory.
-V displays detailed information when executing the command.
-Z only displays the remarks of the compressed file.
-A performs necessary character conversion on text files.
-B. do not convert characters in text files.
-C: The file name in the compressed file is case sensitive.
-J does not process the original directory path of the compressed file.
-L change all file names in the compressed file to lowercase letters.
-M sends the output result to the more Program for processing.
-N do not overwrite the original file during decompression.
-O does not need to ask the user first. after unzip is executed, it overwrites the original file.
-P <密码> Use the zip password option.
-No information is displayed when q is executed.
-S converts the white space characters in the file name to the bottom line characters.
-V retains the file version information of VMS.
-X: saves the original UID/GID of the file at the same time during decompression.
. ZIP file] specifies the. zip compressed file.
[File] specifies the files in the. zip compressed file to be processed.
-D <目录> Specifies the directory to be stored after the file is decompressed.
-X <文件> Specifies which files in the. zip compressed file are not processed.
-Z unzip-Z is equivalent to executing the zipinfo command.
Example:
The zip command can be used to compress files into common zip formats. The unzip command is used to decompress the zip file.
1. I want to compress a file abc.txtand a directory dir1into yasuo.zip:
# Zip-r yasuo.zip abc.txt dir1
2. I downloaded a yasuo.zip file and want to decompress it:
# Unzip yasuo.zip
3. I have abc1.zip, abc2.zip, and abc3.zip under the current directory. I want to decompress them together:
# Unzip abc \?. Zip
Note :? Represents a single character. if "*" is used, it indicates any number of characters.
4. I have a large compressed file large.zip. I don't want to decompress it. I just want to see what it contains:
# Unzip-v large.zip
5. I downloaded a compressed file large.zip and want to verify whether the downloaded file is complete.
# Unzip-t large.zip
6. I used -v8.0 to find that there are many directories and subdirectories in the music.zip compressed file, and the subdirectories are actually mp3 files. I want to download these files to the first-level directory instead of the first-level Directory:
# Unzip-j music.zip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.