SoFunction
Updated on 2025-03-09

Detailed explanation of the tar command under linux

tar command

[root@Linux ~]# tar [-cxtzjvfpPN] Files and Directories ....

parameter:

-c: Create a parameter instruction to create a compressed file;
-x: Unlock the parameter instruction of a compressed file!
-t : View the files in the tarfile!
Pay special attention that in the issuance of parameters, only one can exist in c/x/t! It cannot exist at the same time!
Because it is impossible to compress and decompress at the same time.
-z: Does it have the properties of gzip at the same time? That is, do I need to use gzip compression?
-j: Does it have the attribute of bzip2 at the same time? That is, do I need to use bzip2 to compress?
-v: Display files during compression! This is commonly used, but is not recommended for background execution!
-f: Use the file name, please pay attention, you must immediately follow the file name after f! Don't add parameters!
For example, using 'tar -zcvfP tfile sfile' is the wrong way to write it
『tar -zcvPf tfile sfile』 is the right one!
-p: Use the original attributes of the original file (the attributes will not change according to the user)
-P: You can use absolute paths to compress!
-N: It is even newer than the subsequent date (yyyy/mm/dd) before it will be packaged into the newly created file!
--exclude FILE: Do not package FILE during compression!

example:

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

[root@linux ~]# tar -cvf /tmp/ /etc <== Package only, not compressed!
[root@linux ~]# tar -zcvf /tmp/ /etc <==After packing, compress with gzip
[root@linux ~]# tar -jcvf /tmp/.bz2 /etc <== After packaging, compress with bzip2
# Pay special attention that the file name after parameter f is taken by ourselves, and we are used to use .tar as identification.
# If the z parameter is added, . or .tgz represents the gzip compressed tar file ~
# If you add the j parameter, use .tar.bz2 as the attachment name~
# 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 out what files are in the above /tmp/ file?

[root@linux ~]# tar -ztvf /tmp/
# Since we use gzip compression, when we want to check the files in the tar file,
# You have to add the parameter z! This is very important!

Example 3: Unzip the /tmp/ file under /usr/local/src

[root@linux ~]# cd /usr/local/src
[root@linux src]# tar -zxvf /tmp/
# Under preset situations, we can unzip the compression file anywhere! In this example,
# I first transform the working directory to /usr/local/src and unwrap /tmp/ ,
# Then the unwritten directory will be in /usr/local/src/etc! Also, 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 untie etc/passwd in /tmp/

[root@linux ~]# cd /tmp
[root@linux tmp]# tar -zxvf /tmp/ etc/passwd
# I can check the file name in the tarfile through tar -ztvf. If only one file is needed,
# You can issue this method! Note! The root directory inside / is removed!

Example 5: Back up all files in /etc/ and save their permissions!

[root@linux ~]# tar -zxvpf /tmp/ /etc
# The properties of this -p are important, especially when you want to preserve the properties of the original file!

Example 6: In /home, only new files compared to 2005/06/01 are backed up

[root@linux ~]# tar -N '2005/06/01' -zcvf /home

Example 7: I want to backup /home, /etc, but don't /home/dmtsai

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

Example 8: Package /etc/ and unpack it directly under /tmp without generating files!

[root@linux ~]# cd /tmp
[root@linux tmp]# tar -cvf - /etc | tar -xvf -
# This action is a bit like cp -r /etc /tmp~ It still has its own purpose!
# The thing to note is that the output file becomes - and the input file also becomes -, and there is another | exist~
# These represent standard output, standard input and pipeline commands respectively!
# We will mention this instruction again in this part when we are in the Bash shell!

Summarize

The above is a detailed explanation of the tar command under Linux introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!