Summary of tar commands in Linux system

  
                

Many people do not know the specific usage of the tar command in Linux. In fact, the tar command has many practical places to combine various parameters. The following small series will give you a detailed introduction to the usage of the tar command in Linux. Let's understand it. .

tar Syntax:

syntax: tar [options main and auxiliary + Option] file or directory

Using this command, the main option is a must have, It tells tar what to do, and the secondary options are auxiliary and optional.

Main Options:

c Create a new archive. If the user wants to back up a directory or some files, choose this option. Equivalent to packaging.

x Release the file from the archive. Equivalent to unpacking.

t List the contents of the archive and see which files have been backed up.

It is important to note that c/x/t can only exist in the release of parameters! Can not exist at the same time! Because it is impossible to compress and decompress at the same time.

Auxiliary Options:

-z : Do you have both gzip attributes? Is it necessary to compress or decompress with gzip? The general format is xx.tar.gz or xx. tgz

-j : Does it have the attributes of bzip2 at the same time? That is, do you need to compress or decompress with bzip2? The general format is xx.tar.bz2

-v : Display files during compression! This commonly used

-f : Use the file name, please note that you should pick up the file name immediately after f! Do not add any other parameters!

-p : Use the original properties of the original file (the properties will not change according to the user)

--exclude FILE: During the compression process, do not package FILE!

Example:

Example 1: Package all files in the /etc directory to /tmp/etc.tar

The code is as follows:

[ Root@linux ~]# tar -cvf /tmp/etc.tar /etc "==Package only, no compression! "/p" "p"[root@linux~]# tar -zcvf /tmp/etc.tar.gz /etc "== After packaging, compress "/p" with gzip "p"[root@linux~]# Tar -jcvf /tmp/etc.tar.bz2 /etc "== After packaging, compress "/p" with bzip2 "p"# Pay special attention to the file name after the parameter f is taken by ourselves, we are used to Use .tar for identification. "/p" "p"# If you add the z parameter, use .tar.gz or .tgz to represent the gzip compressed tar file ~ "/p" "p"# If you add the j parameter, then .tar.bz2 As the file name ah ~ "/p" "p" # The above instructions will display a warning message when executed: "/p" "p" # 『tar: Removing leading `/“ from member names』 It is a special setting about the absolute path.

Example 2: Looking at the files in the /tmp/etc.tar.gz file above?

The code is as follows:

[root@linux ~]# tar -ztvf /tmp/etc.tar.gz"/p" "p"# Since we use gzip compression, we have to check When the file in the tar file is "/p" "p"#, you have to add the z parameter! This is very important!

Example 3: Extract the /tmp/etc.tar.gz file under /usr/local/src

The code is as follows:

[root@linux ~] # cd /usr/local/src"/p" "p"[root@linux src]# tar -zxvf /tmp/etc.tar.gz"/p" "p"# In the default case, we can Unzip the archive anywhere! In this example, "/p" "p"# I will first convert the working directory to /usr/local/src and unpack /tmp/etc.tar.gz "/p" "p"# The directory will be in /usr/local/src/etc. In addition, if you enter /usr/local/src/etc "/p" "p"#, you will find that the file attributes in this directory and /etc/may be Different!

Example 4: Under /tmp, I only want to unpack etc/passwd in /tmp/etc.tar.gz. The

code is as follows:

[root@linux ~]# cd /tmp"/p" "p"[root@linux tmp]# tar -zxvf /tmp/etc.tar.gz etc/passwd"/p" "p"# I can Tar -ztvf to check the file name in the tarfile, if you only need one file, "/p" "p"# can be released in this way! Notice! The root directory /etc.tar.gz is removed!

Example 5: I want to back up /home, /etc, but don't /home/dmtsai

The code is as follows:

[root@linux ~]# tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc

In addition: the C parameter of the tar command

The code is as follows:

$ tar -cvf file2 .tar /home/usr2/file2

tar: Removing leading ‘/’ from members names

home/usr2/file2

This command can be /home/The usr2/file2 file is packaged into file2.tar in the current directory. Note that the source file identified by the absolute path is compressed with the tar command. The file name is along with the absolute path (here is home/usr2/, the root directory). ‘/’ was automatically removed) and was compressed. After decompressing with the tar command, the following happens:

The code is as follows:

$ tar -xvf file2.tar

$ ls

…&hellip ; …… home …… ……

The unzipped filename is not the expected file2, but home/usr2/file2.

The code is as follows:

$ tar -cvf file2.tar -C /home/usr2 file2

The -C dir parameter in the command will be the tar working directory from Change the current directory to /home/usr2 and compress the file2 file (without absolute path) to file2.tar. Note: The purpose of the -C dir parameter is to change the working directory, which is valid for the next time before the -C dir parameter in the command.

Using tar's -C dir parameter, you can also extract the files to other directories in the current directory /home/usr1, for example:

The code is as follows:

$ tar -xvf file2.tar -C /home/usr2

While tar does not use the -C dir parameter, it is not possible:

The code is as follows:

$ Tar -xvf file2.tar /home/usr2

tar: /tmp/file: Not found in archive

tar: Error exit delayed from previous errors

The above is Linux The usage of the tar command is introduced. The usage of the tar command is explained in this article. If you don't know how to use the tar command, you may wish to read the contents of this article.

Copyright © Windows knowledge All Rights Reserved