In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use the tar command in linux system. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The Linux tar command is a powerful weapon when archiving or distributing files. The GNU tar archive package can contain multiple files and directories, retain its file permissions, and support a variety of compressed formats. Tar stands for "Tape Archiver", which is the POSIX standard.
The syntax of the tar command is as follows: # tar
Here are some common options in the tar command:
-delete: delete from the archive file (not tape)-r,-append: append the file to the archive file-t,-list: list the contents contained in the archive file-test-label: test the archive file volume label and exit-u,-update: append the updated file to the archive file-x,-extract -get: release the files and directories in the archive-C,-directory=DIR: change the working directory to DIR-f before performing the archive action,-file=ARCHIVE: specify (to be created or already exists) archive file name-j,-bip2: use bzip2 compression for archive files-J,-xz: use xz compression for archive files-p -preserve-permissions: retain access to the original file-v,-verbose: display the entire execution of the command-z, gzip: use gzip compression for archived files
Note: the connectors in the tar command options-optional, no-are fine. This is rare in GNU software, probably because tar commands are more influenced by the ancient UNIX style.
Example 1: create an tar archive file
Now create a tar file and package the / etc/ directory and / root/anaconda-ks.cfg file.
[root@linuxtechi ~] # tar-cvf myarchive.tar / etc / root/anaconda-ks.cfg
The above command creates a tar file named "myarchive" in the current directory, containing the / etc/ directory and the / root/anaconda-ks.cfg file. Where the-c option indicates that you want to create the tar file, the-v option outputs the detailed process of the tar to the screen, and the-f option specifies the archive file name.
[root@linuxtechi ~] # ls-l myarchive.tar-rw-r--r--. 1 root root 22947840 Sep 7 00:24 myarchive.tar [root@linuxtechi ~] # example 2: list the contents of the archive file
Using the-t option in the tar command allows you to quickly list the contents of a file without releasing it.
[root@linuxtechi ~] # tar-tvf myarchive.tar
Lists the specified files and directories in the tar file. The following command attempts to see if the anaconda-ks.cfg file exists in the tar file.
[root@linuxtechi ~] # tar-tvf myarchive.tar root/anaconda-ks.cfg-rw- root/root 2016-08-24 01:33 root/anaconda-ks.cfg [root@linuxtechi ~] # example 3: append a file to an tar file
* *-r: * * is used to append files to existing tar files. Let's add / etc/fstab to the data.tar.
[root@linuxtechi ~] # tar-rvf data.tar / etc/fstab
Note: you cannot append a file in a compressed tar file.
Example 4: release files and directories from a tar file
* *-x: * * is used to release files and directories in tar files. Let's release the contents of the tar file created above.
[root@linuxtechi ~] # tar-xvf myarchive.tar
This command releases the contents of the myarchive.tar file in the current directory.
Example 5: release the tar file to the specified directory
If you want to release the contents of the tar file to the specified folder or directory, use the * *-C: * * option followed by the specified file path.
[root@linuxtechi ~] # tar-xvf myarchive.tar-C / tmp/ example 6: release the specified file or directory in the tar file
Suppose you just release the anaconda-ks.cfg in the tar file to the / tmp directory. The syntax is as follows:
# tar- xvf {tar-file} {file-to-be-extracted}-C {path-where-to-extract} [root@linuxtechi tmp] # tar- xvf / root/myarchive.tar root/anaconda-ks.cfg- C / tmp/root/anaconda-ks.cfg [root@linuxtechi tmp] # ls-l / tmp/root/anaconda-ks.cfg-rw-. 1 root root 953 Aug 24 01:33 / tmp/root/anaconda-ks.cfg [root@linuxtechi tmp] # master tar command to make you big in seconds master tar command to make you big in seconds example 7: create and compress an archive file (.tar.gz or .tgz)
Suppose we need to package the / etc and / opt folders and compress them with the gzip tool. You can do this using the-z option in the tar command. The extension of this tar file can be .tar.gz or .tgz.
[root@linuxtechi ~] # tar-zcpvf myarchive.tar.gz / etc/ / opt/
Or:
[root@linuxtechi ~] # tar-zcpvf myarchive.tgz / etc/ / opt/ example 8: create and compress an archive file (.tar.bz2 or .tbz2)
Suppose we need to package the / etc and / opt folders and use bzip2 compression. You can do this with the-j option in the tar command. The extension of this tar file can be .tar.bz2 or .tbz.
[root@linuxtechi ~] # tar-jcpvf myarchive.tar.bz2 / etc/ / opt/
Or:
[root@linuxtechi ~] # tar-jcpvf myarchive.tbz2 / etc/ / opt/ example 9: create a tar file after excluding a specified file or type
When creating a tar file, use the-exclude option in the tar command to exclude the specified file or type. Suppose you want to exclude .html files when creating a compressed tar file.
[root@linuxtechi ~] # tar-zcpvf myarchive.tgz / etc/ / opt/-- exclude=*.html example 10: list the contents of .tar.gz or .tgz files
Use the-t option to view the contents of a .tar.gz or .tgz file. As follows:
[root@linuxtechi] # tar-tvf myarchive.tgz | more.drwxr-xr-x root/root 0 2016-09-07 08:41 etc/-rw-r--r-- root/root 541 2016-08-24 01:23 etc/fstab-rw- root / root 0 2016-08-24 01:23 etc/crypttablrwxrwxrwx root/root 0 2016-08-24 01:23 etc/mtab-> / proc/self/mounts-rw-r--r-- root/root 2016-09-07 08:41 etc/resolv.confdrwxr-xr-x root/root 0 2016-09-06 03:55 etc/pki/drwxr-xr-x root/root 0 2016-09-06 03:15 etc / pki/rpm-gpg/-rw-r--r-- root/root 1690 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7-rw-r--r-- root/root 1004 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7-rw-r--r-- root/root 1690 2015-12-09 04:59 etc/pki/rpm-gpg/ RPM-GPG-KEY-CentOS-Testing-7-rw-r--r-- root/root 3140 2015-09-15 06:53 etc/pki/rpm-gpg/RPM-GPG-KEY-foreman... Example 11: list the contents of a .tar.bz2 or .tbz2 file
Use the-t option to view the contents of a .tar.bz2 or .tbz2 file. As follows:
[root@linuxtechi ~] # tar-tvf myarchive.tbz2 | more...rwxr-xr-x root/root 0 2016-08-24 01:25 etc/pki/java/lrwxrwxrwx root/root 0 2016-08-24 01:25 etc/pki/java / cacerts-> / etc/pki/ca-trust/extracted/java/cacertsdrwxr-xr-x root/root 0 2016-09-06 02:54 etc/pki/nssdb/-rw-r--r-- root/root 65536 2010-01-12 15:09 etc/pki/nssdb/cert8.db-rw-r--r-- root/root 9216 2016-09-06 02:54 etc/pki/nssdb/cert9.db-rw-r--r-- root/root 16384 2010-01-12 16:21 etc/pki/nssdb/key3.db-rw-r--r-- root/root 11264 2016-09-06 02:54 etc/pki/nssdb/key4.db-rw-r--r-- root/root 451 2015-10-21 09:42 etc/pki/nssdb/pkcs11.txt-rw-r--r-- root/root 16384 2010-01-12 15:45 etc/pki/nssdb/secmod.dbdrwxr-xr- X root/root 0 2016-08-24 01:26 etc/pki/CA/drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/CA/certs/drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/CA/crl/drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/CA/newcerts/drwx- root/root 0 2015-06-29 08:48 etc/pki/CA/private/drwx- root/root 0 2015-11-20 06:34 etc/pki/rsyslog/drwxr-xr-x root/root 0 2016-09-06 03:44 etc/pki/pulp/... . Example 12: extract .tar.gz or .tgz files
Use the-x and-z options to extract .tar.gz or .tgz files. As follows:
[root@linuxtechi] # tar-zxpvf myarchive.tgz-C / tmp/
The above command unzips the tar file to the / tmp directory.
Note: today's tar command automatically checks the compression type of the file before performing the decompression action, which means that we do not have to specify the compression type of the file when using the tar command. As follows:
[root@linuxtechi ~] # tar-xpvf myarchive.tgz-C / tmp/ example 13: extract .tar.bz2 or .tbz2 files
Use the-j and-x options to extract .tar.bz2 or .tbz2 files. As follows:
[root@linuxtechi] # tar-jxpvf myarchive.tbz2-C / tmp/
Or:
[root@linuxtechi ~] # tar xpvf myarchive.tbz2-C / tmp/ example 14: use the tar command for scheduled backups
There are always some real-time scenarios that require us to package the specified files and directories, which has achieved the purpose of daily backup. Assuming that you need to back up the entire / opt directory every day, you can create a cron task with the tar command to do it. As follows:
[root@linuxtechi] # tar-zcvf optbackup-$ (date +% Y-%m-%d) .tgz / opt/
Just create a cron task for the above command.
Example 15: create a compressed archive file using-T and-X
Imagine a scenario in which you record the files and directories you want to archive and compress to a file, and then archive the file as an incoming parameter to the tar command; sometimes you need to exclude the specific path recorded in the file mentioned above and archive and compress it.
Use-T to specify the input file in the tar command,-X to specify a list of files to exclude, suppose you want to archive the / etc, / opt, / home directories, and exclude / etc/sysconfig/kdump and / etc/sysconfig/foreman files, you can create / root/tar-include and / root/tar-exclude and enter the following, respectively:
[root@linuxtechi ~] # cat / root/tar-include/etc/opt/home [root@linuxtechi] # [root@linuxtechi ~] # cat / root/tar-exclude/etc/sysconfig/kdump/etc/sysconfig/foreman [root@linuxtechi ~] #
Run the following command to create a compressed archive file.
[root@linuxtechi ~] # tar zcpvf mybackup-$ (date +% Y-%m-%d) .tgz-T / root/tar-include-X / root/tar-exclude example 16: view the size of .tar, .tgz and .tbz2 files
Use the following command to view (compress) the volume of the tar file.
[root@linuxtechi ~] # tar-czf-data.tar | wc-c427 [root@linuxtechi ~] # tar-czf-mybackup-2016-09-09.tgz | wc-c37956009 [root@linuxtechi ~] # tar-czf-myarchive.tbz2 | wc-c30835317 [root@linuxtechi ~] # example 17: divide a large tar file into multiple small files
The split command is used in Unix-like systems to split large files into small ones. Of course, you can also use this command to split a large tar. Suppose you need to split "mybackup-2016-09-09.tgz" into small files of 6 MB each.
Syntax: split-b. "prefix-name" [root@linuxtechi ~] # split-b 6M mybackup-2016-09-09.tgz mybackup-parts
The above command will split the mybackup-2016-09-09.tgz file into multiple 6-MB small files named mybackup-partsaa ~ mybackup-partsag in the current directory. If you want to distinguish by numbers rather than letters after splitting the file, you can use the-d option in the split command above.
[root@linuxtechi ~] # ls-l mybackup-parts*-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsaa-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsab-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsac-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsad-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsae-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsaf-rw-r--r--. 1 root root 637219 Sep 10 03:05 mybackup-partsag [root@linuxtechi ~] #
These split files are then transferred to other servers over the network, and can be merged into a separate tar file, as follows:
[root@linuxtechi ~] # cat mybackup-partsa* > mybackup-2016-09-09.tgz [root@linuxtechi ~] #
Thank you for reading! This is the end of the article on "how to use tar commands in the linux system". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.