tar [-cxtzjvfpPN] files and directories....
Parameters:
-c: Parameter command to create a compressed file (meaning create);
-x: Parameter command to unzip a compressed file!
-t: View the files in tarfile!
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.
-z: Does it also have the attributes of gzip? That is, do I need to use gzip compression?
-j: Does it also have the attributes of bzip2? That is, do I need to use bzip2 compression?
-v: Display files during compression! This is commonly used, but it is not recommended to be used in background execution processes!
-f: Use the file name, please note that the file name must be followed immediately after f! Don't add any more parameters!
For example, using "tar -zcvfP tfile sfile" is wrong. It should be written as
"tar -zcvPf tfile sfile"!
-p: Use the original attributes of the original file (the attributes will not change according to the user)
-P: You can use absolute paths for compression!
-N: Only the date that is newer than the following date (yyyy/mm/dd) will be packaged into the newly created file!
--exclude FILE: During the compression process, do not package FILE!
Example:
Example 1: Pack all the files in the entire /etc directory into /tmp/etc.tar
[root@ www.linuxidc.com ~]# tar -cvf /tmp/etc.tar /etc<== Only packaging, not compression!
[root@ www.linuxidc.com ~]# tar -zcvf /tmp/etc.tar.gz /etc<==After packaging, compress it with gzip
[root@ www.linuxidc.com ~]# tar -jcvf / tmp/etc.tar.bz2 /etc<==After packaging, compress it with bzip2
# Pay special attention to the fact that the file name after the parameter f is chosen by yourself. We are accustomed to use .tar as the identification.
# If the z parameter is added, .tar.gz or .tgz will be used to represent the gzip compressed tar file ~
# If the j parameter is added, .tar.bz2 will be used as the file extension name~
# The above command is in When executed, a warning message will be displayed:
# 『tar: Removing leading `/" from member names』 That is a special setting about the absolute path.
Example 2: Check the above /tmp/etc.tar.gz file What files are there?
[root@ www.linuxidc.com ~]# tar -ztvf /tmp/etc.tar.gz
# Since we use gzip compression, when we want to check the files in the tar file,
# is You need to add the z parameter! This is very important!
Example 3: Extract the /tmp/etc.tar.gz file under /usr/local/src
[root@ www.linuxidc.com ~] # cd /usr/local/src
[root@ www.linuxidc.com src]# tar -zxvf /tmp/etc.tar.gz
# By default, we can unzip the compressed file anywhere Yes! In this example,
# I first change the working directory to /usr/local/src and unzip /tmp/etc.tar.gz,
# then the untied 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: In /tmp Bottom, I just want to unlock etc/passwd in /tmp/etc.tar.gz
[root@ www.linuxidc.com ~]# cd /tmp
[root@ www.linuxidc.com 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 need one file,
# I can download it this way! ! The root directory / in etc.tar.gz has been removed!
Example 5: Back up all the files in /etc/ and save their permissions!
[root@ www.linuxidc.com ~]# tar -zxvpf /tmp/etc.tar.gz /etc
# This -p attribute is very important, especially when you want to retain the attributes of the original file!
Example 6: In /home, only files newer than 2005/06/01 will be backed up
[root@ www.linuxidc.com ~]# tar -N "2005/06/01" -zcvf home.tar.gz /home
Example 7: I want to back up /home, /etc, but not /home/dmtsai
[root@ www.linuxidc.com ~]# tar --exclude /home/dmtsai -zcvf myfile.tar.gz / home/* /etc
Example 8: Pack /etc/ and unpack it directly under /tmp without generating a file!
[root@ www.linuxidc.com ~]# cd /tmp
[root@ www.linuxidc.com tmp]# tar -cvf - /etc | tar -xvf -
# This action is a bit like cp -r /etc /tmp~ It still has its uses!
# What should be noted is that the output file becomes - and the input file also becomes -, and there is another | ~
# These represent standard output, standard input and pipeline commands respectively!
For more detailed explanations of the tar command under Linux, please pay attention to the PHP Chinese website!