Extraction Syntax: tar [main options + auxiliary options] file or directory. When using this command, the main option is mandatory — it tells tar what to do. The auxiliary options are supporting ones and are optional.
Main options:
c Create a new archive file. If the user wants to back up a directory or some files, this is the option to choose. Equivalent to packing.
x Release files from the archive file. Equivalent to unpacking.
t List the contents of the archive file, to see which files have already been backed up.
Note in particular that among the parameters issued, only one of c/x/t can exist! They cannot exist at the same time! Because it’s impossible to compress and decompress at the same time.
Auxiliary options:
-z : Does it also have the gzip property? In other words, does it need to compress or decompress with gzip? The usual format is xx.tar.gz or xx. tgz
-j : Does it also have the bzip2 property? In other words, does it need to compress or decompress with bzip2? The usual format is xx.tar.bz2
-v : Show the files during compression! This one is commonly used -f : Use a file name — note that the file name must follow immediately after f! Don’t add any other parameters!
-p : Use the original file’s original attributes (the attributes will not change according to the user) —exclude FILE: During compression, do not pack FILE!
Examples: Example one:
Pack all files under the entire /etc directory into /tmp/etc.tar
tar -cvf /tmp/etc.tar /etc
<== pack only, no compression! tar -zcvf /tmp/etc.tar.gz /etc
<== after packing, compress with gzip tar -jcvf /tmp/etc.tar.bz2 /etc
<== after packing, compress with bzip2
Note in particular that the file name after the parameter f is one you choose yourself; we customarily use .tar to identify it.
If you add the z parameter, use .tar.gz or .tgz to represent the gzip-compressed tar file ~
If you add the j parameter, use .tar.bz2 as the extension ~
When the above command is executed, it will display a warning message: # “tar: Removing leading /“ from member names” — that is a special setting regarding absolute paths.
Example two:
Check which files are inside the above /tmp/etc.tar.gz file?
tar -ztvf /tmp/etc.tar.gz
Since we compressed with gzip, when we want to look at the files inside that tar file,
we have to add the z parameter! This is very important!
Example three:
Extract the /tmp/etc.tar.gz file under /usr/local/src
cd /usr/local/src
tar -zxvf /tmp/etc.tar.gz
By default, we can extract the archive anywhere! In this example
I first change the working directory to /usr/local/src and extract /tmp/etc.tar.gz
then the extracted directory will be /usr/local/src/etc. Also, if you enter /usr/local/src/etc
you will find that the file attributes under that directory may differ from those in /etc/!
Example four:
Under /tmp, I only want to extract etc/passwd from /tmp/etc.tar.gz
cd /tmp
tar -zxvf /tmp/etc.tar.gz etc/passwd
I can use tar -ztvf to look at the file names inside the tarfile, and if I want just a single file,
I can issue it this way! Note! The root directory / inside etc.tar.gz has been stripped!
Example five:
I want to back up /home, /etc, but not /home/dmtsai
tar —exclude /home/dmtsai -zcvf myfile.tar.gz /home/ /etc
Also: the C parameter of the tar command
tar -cvf file2.tar /home/usr2/file2
tar: Removing leading ‘/‘ from members names home/usr2/file2 This command can pack the file /home/usr2/file2 into file2.tar in the current directory. Note that for source files identified by absolute paths, after compressing with the tar command, the file name is packed in together with the absolute path (here home/usr2/ — the root directory ‘/‘ is automatically removed). After extracting with the tar command, you get the following: $ tar -xvf file2.tar $ ls …… …… home …… …… The extracted file name is not file2 as you might imagine, but home/usr2/file2.
tar -cvf file2.tar -C /home/usr2 file2
The -C dir parameter in this command changes tar’s working directory from the current directory to /home/usr2, compressing the file2 file (without the absolute path) into file2.tar. Note: the -C dir parameter serves to change the working directory, and it remains in effect until the next -C dir parameter in that command. Using tar’s -C dir parameter, you can likewise extract a file into another directory while in the current directory /home/usr1, for example:
tar -xvf file2.tar -C /home/usr2
Whereas 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

