Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Commands for compression and decompression of Linux files

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the "Linux file compression and decompression command" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

1 、 tar:

Tar command: tar [options.] [FILE]...

Enter "tar -?" Get relevant help information. An example is given in the help document of tar:

Example

Tar-cf archive.tar foo bar # creates the archive file archive.tar from the files foo and bar.

Tar-tvf archive.tar # details all files in the archive file archive.tar.

Tar-xf archive.tar # expand all files in the archive archive.tar.

If you want to compress the folder, enter:

Tar-cvf dirArchive.tar / dirName

Similarly, to extract the archive to a specific folder, it is:

Tar-xvf dirArchive.tar / dirName

Let's review. Our ancestors made a folder and put a number of folders and files in it:

Let's first create a tar file and compress a folder (testdir):

Tar-cf testdir.tar testdir

Then list the structure in the package:

Then we append the file to the package:

Of course, there is no need to do the above single-step operation, this file is already in the archive.

Then take a look at the structure in this compressed package:

If you use the-v option in the example, all the file attributes in the package will be listed. Including permissions, creator, byte size, modification time and file name:

Extract from the tar zip file and display a list of files:

Generally speaking, the most commonly used options are the main operation options:

And practice some other uses:

1. When compressing the testdir directory, you need to exclude the test files in the root path of the folder:

2. Compress only those files that are newer than the specified time:

3. Compress all files or folders with the filename prefix "te" (or files with the suffix "st"):

Finally, add some common options:

-p,-- preserve-permissions,-- same-permissions

Retain the permission information to extract files (only for superusers by default)

-- preserve is the same as-p and-s

-- keep the owner relationship consistent when same-owner tries to decompress (this is the default for super users)

-r,-- append appends the file to the end of the archive

-u,-- update appends only files that are newer than the copy in the archive

-k,-- do not replace existing files when keep-old-files is decompressed

-- keep-newer-files

Do not replace existing files that are newer than the copy in the archive

-- no-overwrite-dir retains the metadata of existing directories

-- rewrite existing files when overwrite is decompressed

-- overwrite-dir

Overwrite the metadata of an existing directory when decompressing (default)

-- recursive-unlink clears the directory hierarchy before decompressing the directory

-- remove-files deletes files after adding them to the archive

-N,-- newer=DATE-OR-FILE,-- after-date=DATE-OR-FILE only saves files that are newer than DATE-OR-FILE

2 、 rar:

Rar--

The default package of the Linux system does not include the rar compression tool, so let's install rar first:

Sudo apt-get install rar

You can enter only one (and only one) of the options given, and let's take a look at the list of commands it provides:

Use an example, for example, now we want to do file compression:

After the compression is completed, we can get the specific structure information in the compressed file by using the command l:

You can even output all the information in the compressed file to the console with the command p:

The section provides some functional options.

For example, you can use-ad to update compressed files in an additive way:

Or if you want to exclude a file, you can use the switch-x (note that the string to be excluded should be immediately followed by x without any symbols). For example, we want to exclude all files under the folder root path that start with "te":

And encryption, with the switch-p, and again, the password immediately followed by p:

If you extract the relative file or open the file directly, you will be asked to enter a password:

In addition, there are some common options:

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

Other commands and switches can be found in the help manual.

3 、 zip, 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 are no notes to write, just look at the help documentation.

Detailed explanation of linux zip command

Function description: compress files.

Grammar: zip [- AcdDfFghjJKlLmoqrSTuvVwXyz$] [- b] [- ll] [- n] [- t] [-] [compressed file] [file.] [- I] [- x]

Additional note: zip is a widely used compression program, after the file is compressed, it will produce another compressed file with the ".zip" extension.

Parameters:

-An adjusts the executable automatic unzipping file.

-b specifies the directory where the files are temporarily stored.

-c comment each compressed file.

-d removes the specified file from the compressed file.

-D the directory name is not established in the compressed file.

-f the effect of this parameter is similar to that of specifying the "- u" parameter, but not only updates the existing file, but also adds some files to the compressed file if they do not already exist in the compressed file.

-F attempts to repair a corrupted compressed file.

-g append a compressed file to an existing compressed file instead of creating a new one.

-h online help.

-I only compress files that meet the criteria.

-j saves only the file name and its contents, but does not store any directory names.

-J deletes unnecessary data in front of the compressed file.

-k uses file names in MS-DOS-compatible format.

When compressing a file, replace LF characters with LF+CR characters.

-ll replaces LF+CR characters with LF characters when compressing the file.

-L displays copyright information.

-m after compressing the file and adding the compressed file, delete the original file, that is, move the file to the compressed file.

-n does not compress files with specific suffix strings.

-o set the change time of the compressed file to the same as the file with the latest change time in the compressed file.

-Q does not show the instruction execution process.

-r Recursive processing, processing all files and subdirectories under the specified directory together.

-S contains system and hidden files.

-t sets the date of the compressed file to the specified date.

-T check that each file in the backup file is correct.

-u replace the newer file into the compressed file.

-v displays instruction execution or version information.

-V saves the file properties of the VMS operating system.

-w if the version number is in the file name, this parameter is valid only under the VMS operating system.

-x excludes eligible files when compressing.

-X does not save additional file properties.

-y saves the symbolic link directly, not the file that the connection points to. This parameter is valid only in systems such as UNIX.

-z comment the compressed file.

-$saves the volume name of the disk where the first compressed file is located.

-Compression efficiency is a value between 1 and 9.

Detailed explanation of linux unzip command

Function description: extract the zip file

Grammar: unzip [- cflptuvz] [- agCjLMnoqsVX] [- P] [.zip file] [file] [- d] [- x] or unzip [- Z]

Supplementary note: unzip is an unzipped program for .zip compressed files.

Parameters:

-c displays the unzipped results on the screen and converts the characters appropriately.

-f update existing files.

-l displays the files contained in the compressed file.

The-p parameter is similar to the-c parameter and displays the result of the decompression to the screen, but no conversion is performed.

-t check that 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 unzipped to a directory.

-v displays detailed information when executing Yes.

-z displays only the remarks text of the compressed file.

-a make the necessary character conversion to the text file.

-b do not convert characters to text files.

-the file name in a compressed C file is case sensitive.

-j does not process the original directory path in the compressed file.

-L changes all file names in the compressed file to lowercase.

-M sends the output to the more program for processing.

-n do not overwrite the original file when unzipping.

-o you do not need to ask the user first, the original file will be overwritten after unzip execution.

-P uses the password option of zip.

-Q does not display any information when executed.

-s converts white space characters in the file name to underscore characters.

-V retains the file version information of VMS.

-X unzips the original UID/GID of the file at the same time.

[.zip file] specifies the .zip zip file.

[file] specifies which files in the .zip zip file are to be processed.

-d specifies the directory to store after the file is unzipped.

-x specifies which files in the .zip zip file should not be processed.

-Z unzip-Z equals to execute zipinfo instruction

Example:

The zip command can be used to compress files into the common zip format. The unzip command is used to extract the zip file.

1. I want to compress a file abc.txt and a directory dir1 into yasuo.zip:

# zip-r yasuo.zip abc.txt dir1

two。 I downloaded a yasuo.zip file and wanted to extract it:

# unzip yasuo.zip

3. I have abc1.zip,abc2.zip and abc3.zip in my current directory, and I want to extract them together:

# unzip abc\? .zip

Note:? Represents one character, if any number of characters are represented by *.

4. I have a large compressed file large.zip. I don't want to unzip it. I just want to see what's in it:

# unzip-v large.zip

5. I downloaded a compressed file large.zip and wanted to verify that the compressed file was fully downloaded

# unzip-t large.zip

6. I use the-v option to find that there are many directories and subdirectories in the music.zip zip file, and the subdirectories are actually songs mp3 files. I want to download these files to the first-level directory instead of building a directory layer by layer:

# unzip-j music.zip

This is the end of the Linux File Compression and decompression Command. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report