Use tar command under linux

  
        

Unpacking

Syntax: tar [main option + sub-options] file or directory

When using this command, the main option is mandatory, it tells tar what to do, The auxiliary option is 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

[root@linux ~]# tar -cvf /tmp/etc.tar /etc <==Package only, no compression!

[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <== After packaging, compress with gzip

[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <== After packaging, compress with bzip2

# Specially note that the file name after the parameter f is taken by itself, we are used to it. .tar for identification.

# If you add the z parameter, use .tar.gz or .tgz to represent the gzip compressed tar file ~

# If you add the j parameter, use .tar.bz2 as the Attached name ah~

# When the above command is executed, a warning message will be displayed:

# 『tar: Removing leading `/" from member names』that is about absolute path Special settings.

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

[root@linux ~]# tar -ztvf /tmp/etc.tar.gz

# Since we use gzip compression, when we look at the files in the tar file,

# You have to add z to this parameter! This is very important!

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

[root@linux ~]# cd /usr/local/src< Br>

[root@linux src]# tar -zxvf /tmp/etc.tar.gz

# In the default case, we can unzip the archive anywhere! In this example,

# I will first convert the working directory to /usr/local/src and unpack /tmp/etc.tar.gz

# Will be in /usr/local/src/etc. In addition, if you enter /usr/local/src/etc

# you will find that the file attributes in this directory may be different from /etc/. Oh!

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

[root@linux ~]# cd /Tmp

[root@linux tmp]# tar -zxvf /tmp/etc.tar.gz etc/passwd

# I can use tar -ztvf to check the file name in the tarfile. If you only need one file,

# 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

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

Also: C parameter for tar command

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

tar: Removing leading '/' from members names

home/usr2/file2

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

$ tar -xvf file2.tar

$ ls

…… …… home …&hellip ; ……

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

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

The -C dir parameter in the command changes the working directory of tar from the current directory to /home/usr2, and compresses 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 another directory in the current directory /home/usr1, for example:

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

and tar does not work with the -C dir parameter:

$ tar -xvf file2.tar /home/usr2

tar: /tmp/file: Not found in archive

tar: Error exit delayed from previous errors

Copyright © Windows knowledge All Rights Reserved