Example of the syntax for using the tar command in linux, linuxtar
Syntax: tar [primary option + secondary option] file or directory
When you use this command, the main option is required, and it tells tar what to do. The auxiliary option is used for assistance and can be used.
Main options:
C: Create a new archive file. Select this option if you want to back up a directory or some files. It is equivalent to packaging.
X: release a file from the file. It is equivalent to unpacking.
T: list the file content and check which files have been backed up.
Note that c/x/t can only exist under the parameter! Cannot exist at the same time! Because it is impossible to simultaneously compress and decompress.
Auxiliary options:
-Z: does it have the gzip attribute at the same time? That is, do I need to use gzip for compression or decompression? The general format is xx.tar.gz or xx. tgz.
-J: Does it have bzip2 attributes at the same time? That is, do I need to use bzip2 for compression or decompression? The general format is xx.tar.bz2.
-V: displays files during compression (this is commonly used ).
-F: use the file name. Please note that the file name should be followed immediately after f! Do not add other parameters !.
-P: use the original attributes of the original file (the attributes are not changed based on the user ).
-- Exclude FILE: do not pack the FILE during compression !.
Example:
Example 1: package all the files in the/etc directory into/tmp/etc.tar (-cvf)
[Root @ linux ~] # Tar-cvf/tmp/etc.tar/etc = pack only, do not compress!
[Root @ linux ~] # Tar-zcvf/tmp/etc.tar.gz/etc = compressed with gzip
[Root @ linux ~] # Tar-jcvf/tmp/etc.tar.bz2/etc = compressed with bzip2
# The file name after parameter f is obtained by ourselves. We use. tar for identification in our habits.
# If the z parameter is added, .tar.gz or. tgz is used to represent the tar file compressed by gzip.
# If you add the j parameter, use .tar.bz2 as the file suffix.
# A warning message is displayed when the preceding command is executed:
# "Tar: Removing leading '/" from member names "is a special setting about absolute paths.
Example 2: Check the files in the above/tmp/etc.tar.gz file? (-Ztvf)
[Root @ linux ~] # Tar-ztvf/tmp/etc.tar.gz
# Because gzip is used for compression, you must add the z parameter when you want to view the files in the tar file.
Example 3: Decompress the/tmp/etc.tar.gz file under/usr/local/src (-zxvf)
[Root @ linux ~] # Cd/usr/local/src
[Root @ linux src] # tar-zxvf/tmp/etc.tar.gz
# 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 extract the etc/passwd in/tmp/etc.tar.gz
[Root @ linux ~] # Cd/tmp
[Root @ linux tmp] # tar-zxvf/tmp/etc.tar.gz etc/passwd
Example 5: I want to back up/home,/etc, but do not/home/dmtsai (-- exclude)
[Root @ linux ~] # Tar -- exclude/home/dmtsai-zcvf myfile.tar.gz/home/*/etc
Notes
$ Tar-cvf file2.tar/home/usr2/file2
This command can package the/home/usr2/file2 file to file2.tar in the current directory. Note that the source file identified by the absolute path is compressed with the tar command, file names are compressed together with absolute paths. After you decompress the package using the tar command, the following situations may occur:
$ Tar-xvf file2.tar
$ Ls
...... ...... Home ...... ......
The extracted file name is not file2, but home/usr2/file2.
Supplement: C parameters of the tar command
To package a single file, use-C instead of folders.
$ Tar-cvf file2.tar-C/home/usr2 file2
In this command, change the tar working directory from the current directory to/home/usr2, and compress the file2 file (without the absolute limit folder to file2.tar. Note: The-C dir parameter only changes the tar working directory. The current directory is not switched to/home/usr2, and its validity period is before the next-C dir parameter in this command.
Using the-C dir parameter of tar, you can also extract the file to another directory in the current directory/home/usr1, for example:
$ Tar-xvf file2.tar-C/home/usr2