Use the tar command under linux

Decompression
syntax: tar [main option + auxiliary option] file or directory

When using this command, the main option is necessary, it tells tar what to do, and the auxiliary option is auxiliary and can be used.

Main options:

c Create a new archive. If the user wants to backup a directory or some files, select this option. Equivalent to packing.

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

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

Special attention, in the parameter assignment, only one c/x/t can exist! cannot exist at the same time! Because it is impossible to compress and decompress at the same time.

Auxiliary options:

-z : Does it also have the gzip attribute? That is, does it need to be compressed or decompressed with gzip? The general format is xx.tar.gz or xx.tgz

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

-v : Display files during compression! This is commonly used

-f : Use the file name, please pay attention to the file name immediately after f! Do not add other parameters!

-p : use the original attributes of the original file (attributes will not change depending on the user)

--exclude FILE: Do not pack FILE during compression!

 

example:

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

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

[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <== packaged and compressed with gzip

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

# Note that the file name after the parameter f is taken by ourselves, and we are used to using .tar as identification.

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

# If the j parameter is added, use .tar.bz2 as the filename~

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

# "tar: Removing leading `/" from member names' is a special setting for absolute paths.

 

Example 2: Check which files are in the above /tmp/etc.tar.gz file?

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

# Since we are using gzip compression, when looking at the files in the tar file,

# You have to add the z parameter! This is important!

 

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

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

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

# By default, we can unpack the archive anywhere! Take this example

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

# The unpacked directory 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/!

 

Example 4: Under /tmp, I just 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 look up the file names in the tarfile through tar -ztvf, if there is only one file,

# It can be issued in this way! Notice! The root directory / in etc.tar.gz has been removed!

 

Example 5: I want to backup /home, /etc but not /home/dmtsai

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

 

Also: C arguments to the 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, It should be noted that the source file identified by the absolute path will be compressed together with the absolute path (here is home/usr2/, the root directory '/' is automatically removed) after being compressed with the tar command . After decompression using the tar command, the following will appear:
  $ tar -xvf file2.tar
  $ ls
  …… …… home …… …… 
  The decompressed file name is not the imagined file2, but home/usr2/file2.

  $ tar -cvf file2.tar -C /home/usr2 file2
  The -C dir parameter in this 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 function of the -C dir parameter is to change the working directory, which is valid until the next -C dir parameter in the command .
  Using the -C dir parameter of tar, you can also extract files to other directories under the current directory /home/usr1, for example:
  $ tar -xvf file2.tar -C /home/usr2
  and tar does not use the -C dir parameter is not possible when:
  $ tar -xvf file2.tar /home/usr2
  tar: /tmp/file: Not found in archive
  tar: Error exit delayed from previous errors

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326390838&siteId=291194637