Linux tar command tutorial

  
 

[root@linux ~]# tar [-cxtzjvfpPN] File & Directory …. Parameters: -c : Create a compressed file parameter command (create meaning); -x : Unpack a compressed file parameter command ! -t : View the files in the tarfile! In particular, in the release of the parameter, c/x/t can only exist one! Can not exist at the same time! Because it is impossible to compress and decompress at the same time. -z : Is it also a property of gzip? That is, do you need to compress with gzip? -j : Is there a property of bzip2 at the same time? That is, do you need to compress with bzip2? -v : Display files during compression! This is commonly used, but it is not recommended for use in the background execution process! -f : Use the file name, please note that you should pick up the file name immediately after f! Do not add parameters! For example, using " tar -zcvfP tfile sfile" is the wrong way to write, you must write " tar -zcvPf tfile sfile"! -p : Use the original attribute of the original file (the attribute will not change according to the user) -P : You can use the absolute path to compress! -N : Newer than the next date (yyyy/mm/dd) will be packaged into the newly created file! –exclude FILE: Do not pack FILE during the compression process! 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# Special attention, the file name after the parameter f is taken by ourselves, we are used to use .tar for identification. # If the z parameter is added, the .tar.gz or .tgz is used to represent the gzip compressed tar file~# If the j parameter is added, the .tar.bz2 is used as the file name ah~# The above instructions are executed. , a warning message will be displayed: # 『tar: Removing leading `/' from member names』 That is a special setting for the absolute path.

Example 2: Check out the files in the above /tmp/etc.tar.gz file? [root@linux ~]# tar -ztvf /tmp/etc.tar.gz# Since we use gzip compression, when we need to look up the files in the tar file, we 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# 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 , #解解的清单(/usr/local/src/etc It! 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 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: Back up all the files in /etc/and save their permissions! [root@linux ~]# tar -zcvpf /tmp/etc.tar.gz /etc# The properties of this -p are important, especially if you want to preserve the properties of the original file!

Example 6: In /home, the new file is backed up than 2005/06/01 [root@linux ~]# tar -N '2005/06/01' -zcvf home.tar.gz /Home

Example 7: I want to back up /home, /etc, but don't /home/dmtsai[root@linux ~]# tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc

Example 8: After /etc/is packaged, it can be directly unpacked under /tmp without generating files! [root@linux ~]# cd /tmp[root@linux tmp]# tar -cvf - /etc

Copyright © Windows knowledge All Rights Reserved