In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Preface
The tar command is used to archive multiple files or directories into a single archive file, which can be further compressed using technologies such as gzip or bzip2.
Command format
Tar [OPTION...] [FILE]...
Command function
Tar (Tape ARchive, the abbreviation of tape archive, originally designed to package files on tape, is now mostly used to back up a partition or some important directories) is the most widely used command in Unix-like systems, it is used to archive multiple files or directories into a single archive file, and archived files can be further compressed using technologies such as gzip or bzip2, while retaining their file permissions. In other words, the tar command can also be used for backup: first archive multiple files and directories to a separate tar file or archive file, and then release the files and directories in the tar file as needed.
Command option
Option meaning-An or-catenate add files to existing backup files-B set block size-c or-create create new backup files-C this option is used to extract, if you want to extract in a specific directory You can use this option-d to record the differences between files-x or-extract or-get to restore the file from the backup file-t or-list to list the contents of the backup file-z or-gzip or-ungzip to process the backup file through the gzip directive-Z or-compress or-uncompress to process the backup file through the compress directive-f or-file= to specify the backup file-v or-verbose display instruction execution-r to add files to already Compressed files-u add changes and existing files to existing compressed files-j support bzip2 unzipped files-v display operation procedure-l file system boundary settings-k keep original files do not overwrite-m keep files not overwritten-w confirm the correctness of compressed files-p or-same-permissions restore files with original file permissions-P or-absolute-names file names use absolute names Do not remove the "/" sign before the file name-N or-newer= will only save files updated from the specified date to the backup file-exclude= excludes files that conform to the template style
What is "file compression"?
We know that in a computer system, the content of a file is information, and the information is actually a sequence of bits (also known as bits) composed of values 0 and 1, and eight bits are organized into groups called bytes. Generally speaking, 8 bits of a byte are not fully utilized, and these unused bits occupy most of the space of a file, while "file compression" is the use of complex computing methods to free up these unused space to make the file take up less space.
To put it simply, "compression" is to fill up the space in the file that is not fully filled. Compressed files cannot be directly used by the operating system, so "unzipping" means to "restore" the file to what it looked like before it was compressed. The ratio of disk space occupied by files before and after compression is the "compression ratio".
Common compression format
Common compression formats in Linux are:
*. Files compressed by Z:compress program.
*. Files compressed by gz:gzip program.
*. Files compressed by bz2:bzip2 program.
The data packaged by the tar:tar program has not been compressed.
* .tar.gz (.tgz): data packaged by a tar program that is compressed by gzip.
* .tar.bz2 (.tbz2): the data packaged by the tar program is compressed by bzip2.
In the above compression format, there are mainly gzip and bzip2 compression commands, which are part of the GNU plan, which was previously the compress command, but it is no longer popular. Bzip2 has a better compression ratio than gzip, but bzip2 usually can only compress and decompress a file. If so, it would be too tedious to compress the entire development environment directory.
So the tar command appears, and tar is not a "compression command", but a "package command". In other words, tar can "package" many files into a single file, even directories. It is true that the tar command does not support compression at first, but later GNU plans to combine the whole tar with compression in order to provide users with more convenient and powerful compression and packaging functions.
Just packaged tar files are commonly known as tarfile files, and compressed tar files are called tarball files.
Omnipotent tar command
Summary
Tar can type multiple directories or files into one large file, and support gzip/bzip2 at the same time
Archiving: tar {- c} [option …] -f destination source
Append archive: tar {- r |-u}-f source [option …] Destination
Decompress: tar {- t |-x}-f source [option …] -C destination
The easiest way to use tar is to keep in mind the following ways:
Compression: tar-jcv-f filename.tar.bz2 the name of the compressed file or directory to view the file: tar-jtv-f filename.tar.bz2 extract: tar-jxv-f filename.tar.gz-C where to extract
Filename.tar.bz2 since tar is not a compression command, but a packaging command, how do you package and compress it? Let's first look at the common parameters of the tar command:
Mode parameter
-c (- create): create a new archive file. -r (- append): creates a new archive file like-c, but this is in append mode and can only be appended to uncompressed archives, requiring the-f parameter to be specified. -t: check which files are contained in the contents of the archive file, and you can see the details, including the file name. -u: same as-r, but only add updated files to the archive. -x: extract the archive file. If there are multiple files with the same file name in an archive, each file will be unzipped first, and the latest file will overwrite the old file.
Tar is divided into three modes,-c _ mam _ r _ limeu, which is archive / compression mode. In this mode, tar recursively traverses all directories and files under the specified directory, and creates archive files. -x is denoted as de-archive / decompression mode, and-t is represented as print list mode.
General parameter
-j: use bzip2 support for compression and decompression, preferably with a file name of * .tar.bz2. -z: use gzip support for compression and decompression, preferably with a file name of * .tar.gz. -v: the name of the file being processed is displayed during the compression / decompression process. -f: followed by the name of the file to be processed, it is best to write a parameter of-f separately. -C: specify the directory to extract. -p: retain the original information of the file, permissions, etc.-P: keep the absolute path when decompressing. -exclude=FILE: do not package FILE when packing and compressing.
Package and create an archive
Example: package a directory.
Description: package the / home/test directory to generate an archive file named command-18-06-02.tar and save it in the current directory.
# tar-cv-f command-18-06-02.tar / home/test/home/test/.bash_logout/home/test/.bashrc/home/test/apache-tomcat-9.0.7.tar.gz/home/test/.bash_profile/home/test/nginx-1.10.1.tar.gz
The-c (short for-create) parameter, which means that a new archive file is created for the specified file or directory. Use-f to specify the archive file to read or write, you can use-to represent standard input or standard output,-f can be written in conjunction with other parameters, and you must make sure that the f parameter is followed by a file name. However, this is not recommended because the parameter exchange order is allowed, and writing-cfv will cause the compressed file name to become v.
Use-v to generate detailed output, and in compressed or decompressed mode, the names of the files being read or written to the archive are listed.
Create a tar.gz archive file
Example: package and use gzip compression.
Description: package all files in the / home/test/images directory and files in the directory and compress them with gzip to generate an archive file named MyImages-18-06-02.tar.gz and put it in the current directory.
# tar-zcv-f MyImages-18-06-02.tar.gz / home/test/imagesOR# tar-zcv-f MyImages-18-06-02.tar.tgz / home/test/images/home/test/images/alejandro-gonzalez-17189.jpg/home/test/images/brooke-lark-275181.jpg/home/test/images/brenda-godinez-228181.jpg/home/test/images/artur-rutkowski-97622.jpg/home/test/images/ben-white-138743.jpg
-z indicates that you want to use gzip support to compress or extract files. Note that the compressed file format of gzip is best written as tar.gz. (note: tar.gz and tgz have the same meaning)
Package and compress to exclude some files
Example: package and compress and exclude some files.
Description: package all files in the / home/test/images directory except brooke-lark-275181.jpg and ben-white-138743.jpg and compress them with gzip to generate an archive file named MyImages-18-06-02.tar.gz and put it in the current directory.
# tar-czv-f MyImages-18-06-02.tar.gz-exclude=./brooke-lark-275181.jpg-- exclude=./ben-white-138743.jpg / home/test/images/home/test/images/alejandro-gonzalez-17189.jpg/home/test/images/brenda-godinez-228181.jpg/home/test/images/artur-rutkowski-97622.jpg
Extract the archive file (default)
Example: decompress, decompress by default.
Description: extract the archive file named MyImages-18-06-02.tar to the current directory.
# tar-xvf MyImages-18-06-02.tarhome/test/images/alejandro-gonzalez-17189.jpghome/test/images/brenda-godinez-228181.jpghome/test/images/artur-rutkowski-97622.jpg
The-x parameter means to extract an archive file. If there are two files with the same name in the archive file, each file will be extracted, and the latest one will overwrite the old file. Note that the-j parameter is not specified here, because when tar sees that the-x parameter is specified, it knows that this is a decompression operation and will automatically determine the compression type of the decompression package.
Extract the archive file and specify a directory
Example: extract to a specified directory.
Description: extract the archive file named MyImages-18-06-02.tar.gz to a specified directory.
# tar-xv-f MyImages-18-06-02.tar-C / home/test/public_imageshome/test/public_images/alejandro-gonzalez-17189.jpghome/test/public_images/brenda-godinez-228181.jpghome/test/public_images/artur-rutkowski-97622.jpg
View compressed package file information
Example: view compressed package file information.
Description: lists the file information in MyImages-18-06-02.tar.bz2, the-v parameter, and produces output similar to the ls (1) command.
# tar-tv-f MyImages-18-06-02.tar.gzOR# tar-tv-f MyImages-18-06-02.tar.bz2Murray RWMUR-root/root 2176861 2018-06-02 21:26 home/test/images/alejandro-gonzalez-17189.jpg-rw-r--r-- root/root 8452524 2018-06-02 21:26 home/test/images/brenda-godinez-228181.jpg-rw-r--r-- root/root 1131986 2018-06-02 21:26 home/test/images/artur-rutkowski-97622.jpg
Extract a single file
Example: extract a single file.
Description: extract the home/test/.bashrc file from the archive.
# tar-xv-f command-18-06-02.tar home/test/.bashrchome/test/.bashrc
Extract multiple specified files
Example: extract multiple specified files.
Description: file1, file2 and other files are extracted from the archive. Multiple files can be separated by spaces or in the form of wildcards.
# tar-zxv-f MyImages-18-06-02.tar.gz "file 1"file 2" OR# tar-zxv-f MyImages-18-06-02.tar.gz-wildcards'* b*.jpg'home/test/images/brooke-lark-275181.jpghome/test/images/brenda-godinez-228181.jpghome/test/images/ben-white-138743.jpghome/test/images/aleks-dahlberg-274646.jpg
Summary
The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.
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.