TrumanWong

tar

Save many files together to a single tape or disk archive and restore the required files individually from the archive.

Supplementary instructions

tar command can create archives for Linux files and directories. Using tar, you can create an archive (backup file) for a specific file, change files in the archive, or add new files to the archive. tar was originally used to create archives on tapes, but now users can create archives on any device. Using the tar command, you can package a large number of files and directories into one file, which is very useful for backing up files or combining several files into one file for network transmission.

First, we need to understand two concepts: packaging and compression. Packaging refers to turning a large number of files or directories into a total file; compression means turning a large file into a small file through some compression algorithms.

Why should we distinguish these two concepts? This is due to the fact that many compression programs in Linux can only compress one file, so when you want to compress a large number of files, you have to first package the large number of files into a package (tar command), and then use Compression program for compression (gzip bzip2 command).

grammar

tar [options...] [FILE]...

Options

-A, --catenate, --concatenate Append tar files to archive
-c, --create create a new archive
-d, --diff, --compare Find archive and file system differences
     --delete Delete from archive (not tape!)
-r, --append Append files to the end of the archive
-t, --list list archive contents
     --test-label test archive volume label and exit
-u, --update Append only files newer than the copy in the archive
-x, --extract, --get Extract files from archive

Operation modifier:

       --check-device Check device number when creating incremental archive (default)
   -g, --listed-incremental=FILE Handle newer GNU format incremental backups
   -G, --incremental Handle old GNU format incremental backups
       --ignore-failed-read
                              Don't exit with a non-zero value when encountering an unreadable file
       --level=NUMBER Output level for incremental list archives created
   -n, --seek archive searchable
       --no-check-device Do not check the device number when creating incremental archives
       --no-seek archive not searchable
       --occurrence[=NUMBER] Process only the NUMBERth occurrence of each file in the archive
                              events; only when used with the following subcommand --delete,
                              --diff, --extract or --list
                              This option is only valid when used in conjunction with one of the . And regardless of whether the file list is given as a command line or via
                              Specified with the -T option; the NUMBER value defaults to 1
       --sparse-version=MAJOR[.MINOR]
                              Sets the discrete format version used (implied
                              --sparse)
   -S, --sparse efficiently process discrete files

  Override control:

   -k, --keep-old-files don't replace existing files when extracting,
                              treat them as errors
       --keep-directory-symlink preserve existing symlinks to directories when
                              extracting
       --keep-newer-files
                              Do not replace existing files that are newer than the copy in the archive
       --no-overwrite-dir preserve metadata for existing directories
       --overwrite overwrite existing files when decompressing
       --overwrite-dir Overwrite metadata of existing directories when decompressing (default)

       --recursive-unlink Clear the directory hierarchy before decompressing the directory
       --remove-files Remove files after adding them to the archive
       --skip-old-files don't replace existing files when extracting,
                              silently skip over them
   -U, --unlink-first delete files to be rewritten before unpacking them
   -W, --verify Attempt to verify the archive after writing

  Select output stream:

       --ignore-command-error Ignore the exit code of the child process
       --no-ignore-command-error
                              Treat a non-zero exit code of a child process as an error
   -O, --to-stdout decompress files to standard output
       --to-command=COMMAND
                              Pipe unzipped files to another program

  Manipulate file properties:

       --atime-preserve[=METHOD]
                              Preserve access times on output files, either by reading in (default
                              Restore the time after METHOD=‘replace’), otherwise do not set the time for the first time (METHOD=‘system’)
       --delay-directory-restore
                              The modification time and permissions of the extracted directory are not set until the end of decompression.
       --group=name force NAME
                              Be the group owner of the added file
       --mode=CHANGES Force the added files (symbols) to change permissions
                              CHANGES
       --mtime=DATE-OR-FILE Set from DATE-OR-FILE for added files
                              mtime
   -m, --touch Do not extract modification time of files
       --no-delay-directory-restore
                              Cancel the effect of the --delay-directory-restore option
       --no-same-owner
                              Unzip the file as your own (default for ordinary users)
       --no-same-permissions
                              Use the user's mask bits when decompressing permissions from archives (default is normal user service)
       --numeric-owner always represents the user/group name numerically
       --owner=name force NAME
                              Be the owner of the added file
   -p, --preserve-permissions, --same-permissions
                              Decompress file permission information (default only for super users)
       --preserve Same as -p and -s
       --same-owner
                              Keep ownership consistent when trying to decompress (default for superuser)
   -s, --preserve-order, --same-order
                              member arguments are listed in the same order as
                              the files in the archive

  Handling of extended file attributes:

       --acls Enable the POSIX ACLs support
       --no-acls Disable the POSIX ACLs support
       --no-selinux Disable the SELinux context support
       --no-xattrs Disable extended attributes support
       --selinux Enable the SELinux context support
       --xattrs Enable extended attributes support
       --xattrs-exclude=MASK specify the exclude pattern for xattr keys
       --xattrs-include=MASK specify the include pattern for xattr keys

  Device selection and switching:

   -f, --file=ARCHIVE Use archive file or ARCHIVE device
       --force-local
                              Even if a copy of the archive file exists, it is still considered a local archive
   -F, --info-script=name, --new-volume-script=name
                              Run script at the end of each tape (implies -M)
   -L, --tape-length=NUMBER Replace tape after writing NUMBER × 1024 bytes
   -M, --multi-volume Create/list/unpack multi-volume archives
       --rmt-command=COMMAND Use the specified rmt COMMAND instead of rmt
       --rsh-command=COMMAND Use remote COMMAND instead of rsh
       --volno-file=FILE Use/update volume number in FILE Device partitioning:

   -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record
   -B, --read-full-records Re-block when reading (only valid for 4.2BSD pipes)
   -i, --ignore-zeros Ignore zero-byte blocks in the archive (i.e. end-of-file)
       --record-size=NUMBER Number of bytes per record NUMBER, multiplied by 512

  Select archive format:

   -H, --format=FORMAT Create an archive in the specified format

  FORMAT is one of the following formats:

     gnu GNU tar 1.13.x format
     oldgnu GNU format as per tar <= 1.12
     pax POSIX 1003.1-2001 (pax) format
     posix is equivalent to pax
     ustar POSIX 1003.1-1988 (ustar) format
     v7 old V7 tar format

       --old-archive, --portability
                              Equivalent to --format=v7
       --pax-option=keyword[[:]=value][,keyword[[:]=value]]...
                              Controlling the pax keyword
       --posix is equivalent to --format=posix
   -V, --label=TEXT Create volume with name TEXT
                              of archives; when listing/extracting, use TEXT
                              Pattern string as volume name

  Compression options:

   -a, --auto-compress Use archive suffix to determine compression program
   -I, --use-compress-program=PROG
                              Filter by PROG (must be able to accept -d
                              option program)
   -j, --bzip2 Filter archives by bzip2
   -J, --xz filter archives by xz
       --lzip Filter archives by lzip
       --lzma Filter archives by lzma
       --lzop
       --no-auto-compress Do not use archive suffix to determine compression program
   -z, --gzip, --gunzip, --ungzip Filter archives by gzip
   -Z, --compress, --uncompress Filter archives by compress

  Local file selection:

       --add-file=FILE Add the specified FILE to the archive (if the name begins with -
                              It will be useful to start with)
       --backup[=CONTROL] Backup before deletion, select CONTROL version
   -C, --directory=DIR change to directory DIR
       --exclude=PATTERN exclude files specified with PATTERN
       --exclude-backups exclude backups and lock files
       --exclude-caches In addition to identifying the file itself, exclude inclusions
                              Contents of the directory CACHEDIR.TAG
       --exclude-caches-all exclude directories containing CACHEDIR.TAG
       --exclude-caches-under exclude everything in the directory containing CACHEDIR.TAG

       --exclude-tag=FILE exclude FILE except FILE itself
                              the contents of the directory
       --exclude-tag-all=FILE exclude directories containing FILE
       --exclude-tag-under=FILE exclude everything in the directory containing FILE
       --exclude-vcs exclude version control system directories
   -h, --dereference
                              Follow symbolic links; archive and output the files they point to
       --hard-dereference
                              Follow hard links; archive and output the files they point to
   -K, --starting-file=MEMBER-NAME
                              begin at member MEMBER-NAME when reading the
                              archive
       --newer-mtime=DATE compare data and time when only data has changed
       --no-null disable last effect --null option
       --no-recursion avoid automatic downgrade in directory
       --no-unquote Do not end with the filename read by -T as a quote
       --null -T reads null-terminated names, -C disables
   -N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE
                              Only save files newer than DATE-OR-FILE
       --one-file-system Save in local file system when creating archive
   -P, --absolute-names Do not remove the boot character '/' from file names
       --recursion directory recursion (default)
       --suffix=STRING Backup before deletion unless specified by environment variable
                              SIMPLE_BACKUP_SUFFIX
                              Override, otherwise cover common suffixes (‘’)
   -T, --files-from=FILE from FILE
                              Get the file name to decompress or create the file
       --unquote with -T
                              Filenames read end as references (default)
   -X, --exclude-from=FILE exclude pattern strings listed in FILE

  File name transformation:

       --strip-components=NUMBER Remove NUMBER from file names when decompressing
                              a boot section
       --transform=EXPRESSION, --xform=EXPRESSION
                              Use sed instead of EXPRESSION
                              to perform file name conversion

  Filename matching options (affects both exclude and include pattern strings):

       --anchored pattern string matches file name header
       --ignore-case ignore case
       --no-anchored pattern string matches any character after '/' (default
                              exclusion is valid)
       --no-ignore-case match case (default)
       --no-wildcards match strings verbatim
       --no-wildcards-match-slash Wildcards do not match ‘/’
       --wildcards use wildcards (default)
       --wildcards-match-slash
                              Wildcard matching ‘/’ (effective for exclusion operations by default)

  Informative output:

       --checkpoint[=NUMBER] every NUMBER
                              Display progress information for records (default is 10)
       --checkpoint-action=ACTION execute ACTION on each checkpoint
       --full-time print file time to its full resolution
       --index-file=FILE Send verbose output to FILE
   -l, --check-links
                              Print information as long as not all links are exported
       --no-quote-chars=STRING disable character quotes from STRING
       --quote-chars=STRING Extra quote characters from STRING
       --quoting-style=STYLE Set name quoting style; valid STYLE
                              Please refer to the following description for the value
   -R, --block-number Each message displays the number of blocks within the archive
       --show-defaults Show tar default options
       --show-omitted-dirs
                              When listing or unpacking, list every directory that does not match the search criteria
       --show-transformed-names, --show-stored-names
                              Display the transformed file name or archive name
       --totals[=SIGNAL] print out the total number of bytes after processing the archive; when this
                              SIGNAL is triggered with parameters -
                              Print the total number of bytes; allowed signals are:
                              SIGHUP, SIGQUIT, SIGINT, SIGUSR1 and
                              SIGUSR2; also accepts without SIG
                              prefixed signal name
       --utc print file modification time in UTC format
   -v, --verbose lists processed files in detail
       --warning=KEYWORD warning control:
   -w, --interactive, --confirmation
                              Require confirmation for every operation

  Compatibility options:

   -o When creating an archive, it is equivalent to
                              --old-archive; when expanding the archive, equivalent to
                              --no-same-owner

  Other options:

   -?, --help Display this help list
       --restrict disable some potentially dangerous options
       --usage displays a brief usage description
       --version print program version

The long option and the corresponding short option have the same mandatory or optional parameters.

Unless specified with --suffix or SIMPLE_BACKUP_SUFFIX
Set the backup suffix, otherwise the backup suffix is "~".
Version control can be set with --backup or VERSION_CONTROL. Possible values are:

   none, off Never make backups
   t, numbered perform number backup
   nil, existing
If numbered backup exists, perform numbered backup, otherwise perform simple backup.
   never, simple Always use simple backup

Valid parameters for the --quoting-style option are:

   literal
   shell
   shell-always
   c
   c-maybe
   escape
   locale
   clocale

This tar defaults to:
--format=gnu -f- -b20 --quoting-style=escape --rmt-command=/etc/rmt
--rsh-command=/usr/bin/ssh

Parameters

File or directory: Specify a list of files or directories to package.

Example

Pack the /home/vivek/bin/ directory and compress it using the gzip algorithm. Save as /tmp/bin-backup.tar.gz file.

tar -zcvf /tmp/bin-backup.tar.gz /home/vivek/bin/
-z: with gzip attribute
-j: with bz2 attribute
-Z: with compress attribute
-v: show all processes
-O: Unpack the file to standard output
tar -cf archive.tar foo bar # Create archive file archive.tar from files foo and bar.
tar -tvf archive.tar # List all files in the archive file archive.tar in detail.
tar -xf archive.tar # Expand all files in the archive file archive.tar.

The following parameter -f is required

-f: Use the file name. Remember, this parameter is the last parameter, and can only be followed by the file name.

tar -cf all.tar *.jpg
# This command is to package all .jpg files into a package named all.tar. -c means generating a new package, and -f specifies the file name of the package.

tar -rf all.tar *.gif
# This command is to add all .gif files to the all.tar package. -r means adding files.

tar -uf all.tar logo.gif
# This command is to update the logo.gif file in the original tar package all.tar. -u means to update the file.

tar -tf all.tar
# This command is to list all files in the all.tar package, -t means to list files.
tar -cvf archive.tar foo bar # Create archive.tar from files foo and bar.
tar -tvf archive.tar # List all files in archive.tar in detail.
tar -xf archive.tar # Extract all files from archive.tar.

zip format

Compression: zip -r [target file name].zip [original file/directory name] Unzip: unzip [original file name].zip Note: -r parameter represents recursion

tar format (this format is only packaged, not compressed)

Packaging: tar -cvf [target file name].tar [original file name/directory name] Unpack: tar -xvf [original file name].tar Note: The c parameter represents create (create), the x parameter represents extract (unpacking), the v parameter represents verbose (details), and the f parameter represents filename (file name), so the file name must be followed by f.

tar.gz format

Method 1: Use the previously packaged tar file and directly use the compression command.

Compression: gzip [original file name].tar Unzip: gunzip [original file name].tar.gz

Method 2: Pack and compress, decompress and unpack all at once

Pack and compress: tar -zcvf [target file name].tar.gz [original file name/directory name] Unzip and unpack: tar -zxvf [original file name].tar.gz Note: z stands for compression/decompression using the gzip algorithm.

tar.bz2 format

Method 1: Use the packaged tar file to directly execute the compression command:

Compression: bzip2 [original file name].tar Unzip: bunzip2 [original file name].tar.bz2 Method 2: Pack and compress, decompress and unpack all at once

Pack and compress: tar -jcvf [target file name].tar.bz2 [original file name/directory name] Unzip and unpack: tar -jxvf [original file name].tar.bz2 Note: Lowercase j means using bzip2 algorithm to compress/decompress.

tar.xz format

Method 1: Use the packaged tar file and directly use the compression command:

Compression: xz [original file name].tar Unzip: unxz [original file name].tar.xz Method 2: Pack and compress, decompress and unpack all at once

Pack and compress: tar -Jcvf [target file name].tar.xz [original file name/directory name] Unzip and unpack: tar -Jxvf [original file name].tar.xz Note: Capital J stands for compression/decompression using the xz algorithm.

tar.Z format (obsolete)

Method 1: Use the packaged tar file and directly use the compression command:

Compression: compress [original file name].tar Unzip: uncompress [original file name].tar.Z Method 2: Pack and compress, decompress and unpack all at once

Pack and compress: tar -Zcvf [target file name].tar.Z [original file name/directory name] Unzip and unpack: tar -Zxvf [original file name].tar.Z Note: Capital Z represents compression/decompression using the ncompress algorithm. In addition, ncompress is the compression format of early Unix systems, but because the compression rate of ncompress is too low, it is now obsolete.

jar format

Compression: jar -cvf [target file name].jar [original file name/directory name] Unzip: jar -xvf [original file name].jar

Note: If you are packaging a Java class library and there is a main class in the class library, you need to write a META-INF/MANIFEST.MF configuration file with the following content:

Manifest-Version: 1.0
Created-By: 1.6.0_27 (Sun Microsystems Inc.)
Main-class: the_name_of_the_main_class_should_be_put_here

Then package it with the following command:

jar -cvfm [target file name].jar META-INF/MANIFEST.MF [original file name/directory name] In this way, you can use the "java -jar [file name].jar" command to directly run the public static void main method in the main class.

7z format

Compression: 7z a [target file name].7z [original file name/directory name] Unzip: 7z x [original file name].7z Note: This 7z decompression command supports rar format, that is:

7z x [original file name].rar

Other examples

Package all files into tarball package:

tar -cvf log.tar log2012.log only packages, not compresses!
tar -zcvf log.tar.gz log2012.log is packaged and compressed with gzip
tar -jcvf log.tar.bz2 log2012.log is packaged and compressed with bzip2

The file name after option f is chosen by yourself, and we are accustomed to use .tar as identification. If the z option is added, .tar.gz or .tgz will be used to represent the gzip compressed tar package; if the j option is added, .tar.bz2 will be used as the tar package name.

Unzip directory

Parameter --strip-components NUMBER, delete NUMBER leading components from the file name when extracting. If you want to remove the first two layers, the parameter is --strip-components 2

tar -xvf portal-web-v2.0.0.tar --strip-components 1 -C specifies the directory

Example

tar -xvf xxx.tar.gz -C /usr/src/a
/usr/src/a/xxxxx/src/opp/b.txt
tar -xvf xxx.tar.gz -strip-components=1 -C /usr/src/a
/usr/src/a/src/opp/b.txt

Check what files are in the above tar package:

tar -ztvf log.tar.gz

Since we use gzip compressed log.tar.gz, when we want to check the files in the log.tar.gz package, we have to add the z option.

Extract the tar package:

tar -zxvf /opt/soft/test/log.tar.gz

By default, we can unzip the compressed file anywhere

Extract only some files in tar:

tar -zxvf /opt/soft/test/log30.tar.gz log2013.log

I can use tar -ztvf to check the file names in the tar package. If I only need one file, I can use this method to decompress part of the file!

Back up the file and save its permissions:

tar -zcvpf log31.tar.gz log2014.log log2015.log log2016.log

The -p attribute is important, especially if you want to preserve the attributes of the original file.

In the folder, only files newer than a certain date will be backed up:

tar -N "2012/11/13" -zcvf log17.tar.gz test

The contents of the backup folder exclude some files:

tar --exclude scf/service -zcvf scf.tar.gz scf/*

Delete source files after packaging files:

tar -cvf test.tar test --remove-files

In fact, the easiest way to use tar is to remember the following method:

Compression: tar -jcv -f filename.tar.bz2 The name of the file or directory to be compressed
Query: tar -jtv -f filename.tar.bz2
Decompression: tar -jxv -f filename.tar.bz2 -C directory to be decompressed