In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use the tar command to compress and extract files in the Linux system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Tar command in linux
The tar (Tape Archive) command is a command that is often used in linux systems to store files in an archive.
Common file extensions include .tar.gz and .tar.bz2, which represent further compression through the gzip or bzip algorithms, respectively.
In this tutorial, we will take a look at some examples of using the tar command in the linux desktop or server version to handle some of the day-to-day work of creating and extracting archive files.
Use the tar command
The tar command is available by default on most linux systems, so you don't have to install the software separately.
The tar command has two compression formats, gzip and bzip, whose "z" option is used to specify gzip and "j" option is used to specify bzip. You can also create uncompressed archive files.
1. Extract an tar.gz archive
The common usage is to extract the archive file, and the following command will extract the file from an tar.gz archive file.
The code is as follows:
$tar-xvzf tarfile.tar.gz
Here is a simple explanation for these parameters-
X-decompress the file
V-verbose mode, printing out the name of each file as it is decompressed.
Z-this file is a file compressed using gzip.
F-use the following tar archive for operation.
These are some important options to keep in mind.
Extract the tar.bz2/bzip archive file
Files with bz2 extensions are compressed using the bzip algorithm, but the tar command can also process them, but you need to replace the "z" option by using the "j" option.
The code is as follows:
$tar-xvjf archivefile.tar.bz2
two。 Extract the file to a specified directory or path
To extract the file to a specified directory, use the "- C" option to specify the path, where "C" is an uppercase "C".
The code is as follows:
$tar-xvzf abc.tar.gz-C / opt/folder/
Then, first you need to make sure that the target directory exists. After all, the tar command will not create a directory for you, so if the target directory does not exist, the command will fail.
3. Extract a single file
To extract a single file from an archive, simply place the file name after the command as follows.
The code is as follows:
$tar-xz-f abc.tar.gz ". / new/abc.txt"
In the above command, you can specify multiple files as follows.
The code is 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, such as all files with the extension ".txt".
The code is as follows:
$tar-xz-f abc.tar.gz-- wildcards "* .txt"
5. List and retrieve the contents of the tar archive
If you just want to list rather than extract the contents of the tar archive, use the "- t" (test) option, the following command is used to print the contents of a tar archive compressed using gzip.
The code is as follows:
$tar-tz-f abc.tar.gz
. / new/
. / new/cde.txt
. / new/subdir/
. / new/subdir/in.txt
. / new/abc.txt
...
You can pipe the output to grep to search for a file, or you can direct to the less command to browse the content list. Using the "v" verbose option will print out additional details for each file.
For tar.bz2/bzip files, you need to use the "j" option.
Retrieve the archive file by combining the above command with the grep command, as shown below. Easy!
The code is as follows:
$tar-tvz-f abc.tar.gz | grep abc.txt
-rw-rw-r-- enlightened/enlightened 0 2015-01-13 11:40. / new/abc.txt
6. Create an tar/tar.gz archive
Now that we have learned how to extract a tar archive, it is time to start creating a new tar archive. The tar command can be used to put the selected file or entire directory into an archive file, and the following is an example.
The following command uses a directory to create an tar archive, which adds all files and subdirectories in that directory to the archive.
The code is as follows:
$tar-cvf abc.tar. / new/
. / new/
. / new/cde.txt
. / new/abc.txt
The above command does not create a compressed archive file, just a normal archive file, but putting multiple files into one archive file does not really compress each file.
To use compression, you can use the "z" or "j" options for gzip or bzip compression algorithms, respectively.
The code is as follows:
$tar-cvzf abc.tar.gz. / new/
The file extension doesn't really matter. "tar.gz" and "tgz" are common extensions for compressed files using the gzip compression algorithm. "tar.bz2" and "tbz" are common extensions for compressed files in the bzip compression algorithm. (LCTT translation note: whether an archive is compressed and which compression method is used does not depend on its extension, it is just for easy identification. ).
7. Confirm before adding a file
A useful option is "w", which allows the tar command to allow the user to confirm before adding each file to the archive, which is sometimes useful.
When using this option, only the file that the user typed "y" will be added to the archive file, and if you do not enter anything, the default representation is an "n".
The code is as follows:
# add 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
# now list all files that have been added
$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 a file to an existing archive file without creating a new one. Here is a simple example:
The code is as follows:
$tar-rv-f abc.tar abc.txt
Files cannot be added to a compressed archive file (gz or bzip). Files can only be added to ordinary archives.
9. Add a file to a compressed archive file (tar.gz/tar.bz2)
It has been mentioned earlier that it is not possible to add files to a compressed archive, but it can still be done with simple tricks. Use the gunzip command to extract the archive file, then add the file to the archive file and recompress it.
The code is as follows:
$gunzip archive.tar.gz
$tar-rf archive.tar. / path/to/file
$gzip archive.tar
Use bzip2 and bunzip2 for bzip files, respectively.
10. Backup via tar
A real scenario is to back up directories within a fixed time interval. The tar command can achieve such a backup through cron scheduling. Here is an example:
The code is as follows:
$tar-cvz-f archive-$ (date +% Y%m%d). Tar.gz. / new/
Running the above command using cron maintains the creation of a backup file with a name similar to the following: 'archive-20150218.tar.gz'.
Of course, you need to ensure that the growing number of archived files does not cause disk space overflows.
11. Validate when creating an archive file
The "W" option can be used to validate after the archive file has been created. Here is a simple example.
The code is as follows:
$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
It is important to note that the validation action cannot be performed on a compressed archive file and can only be performed on an uncompressed tar archive file.
That's it for this time. You can view the manual of the tar command through the "man tar" command.
This is the end of the content of "how to use the tar command to compress and extract files in the Linux system". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.