11 tar command examples for creating and extracting documents in Linux
Tar commands in Linux
The tar (Tape archiving) command is often used in the inux system to save files to an archive file.
Common file extensions include .tar.gz and .tar.bz2, which indicate further compression through gzip or bzip algorithms.
In this tutorial, we will look at some examples of daily work of creating and decompressing archive files using the tar command in Linux desktop or server version.
Use the tar command
The tar command is available in most linux systems by default, so you do not need to install the software separately.
The tar command has two compression formats: gzip and bzip. the "z" option of this command is used to specify gzip, and the "j" option is used to specify bzip. You can also create non-compressed archive files.
1.decompress A tar.gz Archive
The general method is to decompress the archive file. The command below will extract the file from a tar.gz archive file.
$ tar -xvzf tarfile.tar.gz
Here is a simple explanation of these parameters-
X-decompress the file
V-lengthy mode: prints the file name when extracting each file.
Z-this file is a file compressed using gzip.
F-use the following tar archive for operations.
These are important options to remember.
Decompress the archive tar.bz2/bzip File
Files with the bz2 extension are compressed using the bzip algorithm, but the tar command can also process them, but the "z" option needs to be replaced by the "j" option.
$ tar -xvjf archivefile.tar.bz2
2. decompress the file to a specified directory or path.
To decompress the file to a specified directory, use the "-C" option to specify the path, where "C" is capitalized "C ".
$ tar -xvzf abc.tar.gz -C /opt/folder/
First, check whether the target directory exists. After all, the tar command does not create a directory for you. Therefore, if the target directory does not exist, the command will fail.
3. Extract A single file
To extract a single file from an archive file, you only need to place the file name after the command as follows.
$ tar -xz -f abc.tar.gz "./new/abc.txt"
In the preceding command, you can specify multiple files as follows.
$ tar -xz -f abc.tar.gz "./new/cde.txt""./new/abc.txt"
4. Use wildcards to extract multiple files
Wildcards can be used to extract a batch of files that match a given wildcard, for example, all files with the ". txt" extension.
$ tar -xz -f abc.tar.gz --wildcards "*.txt"
5. List and retrieve the content in the tar archive file
If you only want to list the content in the tar archive file instead of decompressing it, use the "-t" (test) option, the following command is used to print the content of a tar archive file compressed by gzip.
$ tar -tz -f abc.tar.gz
./new/
./new/cde.txt
./new/subdir/
./new/subdir/in.txt
./new/abc.txt
...
The output can be directed to grep to search for a file, or directed to the less command to browse the content list. Using the "v" lengthy option will print additional details for each file.
For the tar.bz2/bzip file, use the "j" option.
Use the preceding command and grep command to retrieve the archive file, as shown below. Easy!
$ tar -tvz -f abc.tar.gz | grep abc.txt
-rw-rw-r-- enlightened/enlightened 02015-01-1311:40./new/abc.txt
6. Create a tar/tar.gz archive file
Now we have learned how to decompress a tar archive file. It is time to create a new tar archive file. The tar command can be used to put the selected file or entire directory into an archive file. The following is an example.
The following command uses a directory to create a tar archive file, which adds all the files and subdirectories to the archive file.
$ tar -cvf abc.tar ./new/
./new/
./new/cde.txt
./new/abc.txt
The preceding command does not create a compressed archive file. It is just a common archive file. It only puts multiple files into one archive file and does not actually compress each file.
To use compression, you can use the "z" or "j" options to perform gzip or bzip compression algorithms.
$ tar -cvzf abc.tar.gz ./new/
The file extension does not actually have any effect. “Tar.gz "and" tgz "are common extensions of gzip compression algorithms to compress files. “Tar.bz2 "and" tbz "are common extensions of files compressed by bzip compression algorithms, extension only for easy identification .).
7. Confirm before adding the file
A useful option is "w". This option enables the tar command to allow users to confirm each file before it is added to the archive file, which is sometimes useful.
When this option is used, only files input "y" will be added to the archive file. If you do not enter anything, the default value is "n ".
# Add a specified file
$ tar -czw -f abc.tar.gz ./new/*
add ‘./new/abc.txt’?y
add ‘./new/cde.txt’?y
add ‘./new/newfile.txt’?n
add ‘./new/subdir’?y
add ‘./new/subdir/in.txt’?n
# List all added files
$ tar -t -f abc.tar.gz
./new/abc.txt
./new/cde.txt
./new/subdir/
8. Add a file to an existing archive file
The "r" option can be used to add an existing archive file without creating a new archive file. The following is a simple example:
$ tar -rv -f abc.tar abc.txt
Files cannot be added to compressed archive files (gz or bzip ). Files can only be added to common archive files.
9. Add the file to the compressed archive file (tar.gz/tar.bz2)
It has been mentioned before that it is impossible to add files to compressed archive files. However, it can still be done through some simple tricks. Use the gunzip command to decompress the archive file, add the file to the archive file, and then compress it.
$ gunzip archive.tar.gz
$ tar -rf archive.tar ./path/to/file
$ gzip archive.tar
Bzip2 and bunzip2 are used for bzip files.
10. Back up data using tar
A real scenario is to back up directories at a fixed interval. the tar command can implement such a backup through cron scheduling. The following is an example:
$ tar -cvz -f archive-$(date +%Y%m%d).tar.gz ./new/
Running the preceding command using cron will keep creating a backup file named 'archive-20150218.tar.gz '.
Of course, it is necessary to ensure that the increasing number of archive files does not cause disk space overflow.
11. Verify when creating an archive file
The "W" option can be used for verification after an archive file is created. The following is a simple example.
$ tar -cvW -f abc.tar ./new/
./new/
./new/cde.txt
./new/subdir/
./new/subdir/in.txt
./new/newfile.txt
./new/abc.txt
Verify./new/
Verify./new/cde.txt
Verify./new/subdir/
Verify./new/subdir/in.txt
Verify./new/newfile.txt
Verify./new/abc.txt
Note that the verification action cannot be performed on the compressed archive file, but only on the non-compressed tar archive file.
This is the first time. You can run the "man tar" command to view the manual of the tar command.
This article permanently updates the link address: