How to use the tar command to organize packaging, compression and other operations under Linux

888u

Last update at :2023-12-12,Edit by888u

The Linux system is quite troublesome to operate for those who are not familiar with it. Sometimes it needs to be named this way, sometimes it needs that command, and sometimes... When I encountered a problem with the AMH upgrade today, I performed a data packaging operation. How to use the tar command for packaging, compression, etc. The following content is collected from the Internet and briefly organized.

Main options:

c Create a new archive file. If the user wants to back up a directory or some files, he or she must select this option. Equivalent to packaging.

x Release the file from the archive. It's the equivalent of unpacking.

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

Specially note that in the parameter release, 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.

Accessibility options:

-z: Does it also have the attributes of gzip? 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 attributes of bzip2? That is, do I 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 note that the file name must be followed immediately after f! Do not add other parameters!

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

–exclude FILE: Do not pack FILE during compression!

Example:

Example 1: Pack all the 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 <==After packaging, compress with gzip

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

# Pay special attention to the fact that the file name after parameter f is chosen by yourself. We are accustomed to use .tar as identification.

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

# If you add the j parameter, use .tar.bz2 as the file extension~

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

# 『tar: Removing leading `/” from member names』That is a special setting about absolute paths.

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

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

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

# You have to add the z parameter! This is very 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 unzip the compressed file anywhere! Take this example

# I first change the working directory to /usr/local/src and unzip /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 unzip 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 I only want one file,

# You can submit it through this method! Notice! The root directory / in etc.tar.gz has been removed!

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

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

In addition: C parameter of 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 is compressed with the tar command and the file name together with the absolute path (here is home/usr2/, the root directory '/' is automatically removed) and compressed together. After using the tar command to decompress, the following situation will occur:
$ tar -xvf file2.tar
$ls
…… …… home …… ……
The decompressed file name is not file2 as imagined, 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 the absolute path) into file2.tar. Note: The -C dir parameter is used to change the working directory, and its validity period is before the next -C dir parameter in the command.
Using tar's -C dir parameter, you can also decompress files to other directories in the current directory /home/usr1, for example:
 $ tar -xvf file2.tar -C /home/usr2
However, tar cannot do this without the -C dir parameter:
$ tar -xvf file2.tar /home/usr2
tar: /tmp/file: Not found in archive
tar: Error exit delayed from previous errors

Recommended site searches: domain name query, network registration, domain name query official website, US virtual host, server rental US high defense, IP reverse domain name check, mainland China proxy IP, cloud virtual host, .cn domain name registration, free virtual host server ,

How to use the tar command to organize packaging, compression and other operations under Linux

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码