Linux tar Command
The Linux tar (short for tape archive) command is used for backing up files.
tar is a tool for creating and restoring backup files. It can add to and extract files from backup archives.
Syntax
tar [-ABcdgGhiklmMoOpPrRsStuUvwWxzZ][-b <block number>][-C <destination directory>][-f <backup file>][-F <Script file>][-K <file>][-L <media capacity>][-N <date time>][-T <template file>][-V <volume name>][-X <template file>][-<device number><storage density>][--after-date=<date time>][--atime-preserve][--backup=<backup method>][--checkpoint][--concatenate][--confirmation][--delete][--exclude=<template pattern>][--force-local][--group=<group name>][--help][--ignore-failed-read][--new-volume-script=<Script file>][--newer-mtime][--no-recursion][--null][--numeric-owner][--owner=<user name>][--posix][--preserve][--preserve-order][--preserve-permissions][--record-size=<block number>][--recursive-unlink][--remove-files][--rsh-command=<command>][--same-owner][--suffix=<backup suffix string>][--totals][--use-compress-program=<command>][--version][--volno-file=<number file>][files or directories...]
Parameters:
- -A or --catenate: Add files to an existing backup file.
- -b<block number> or --blocking-factor=<block number>: Set the number of blocks per record, each block being 12 bytes.
- -B or --read-full-records: Reset block size when reading data.
- -c or --create: Create a new backup file.
- -C<destination directory> or --directory=<destination directory>: Change to the specified directory.
- -d or --diff or --compare: Compare the differences between the backup file and the files on the file system.
- -f<backup file> or --file=<backup file>: Specify the backup file.
- -F<Script file> or --info-script=<Script file>: Execute the specified Script file each time a tape is changed.
- -g or --listed-incremental: Handle GNU-format incremental backups.
- -G or --incremental: Handle old GNU-format incremental backups.
- -h or --dereference: Do not create symbolic links; copy the original files that the links point to.
- -i or --ignore-zeros: Ignore zero-byte blocks in the backup file, i.e., EOF.
- -k or --keep-old-files: Do not overwrite existing files when extracting from a backup file.
- -K<file> or --starting-file=<file>: Start restoration from the specified file.
- -l or --one-file-system: The copied files or directories must be on the same file system as the one where the tar command is executed; otherwise, they will not be copied.
- -L<media capacity> or --tape-length=<media capacity>: Set the capacity of the storage medium, calculated in 1024-byte units.
- -m or --modification-time: Do not change the modification time of the files when restoring.
- -M or --multi-volume: Use multi-volume mode when creating, restoring, or listing the contents of backup files.
- -N<date format> or --newer=<date time>: Only save files newer than the specified date to the backup file.
- -o or --old-archive or --portability: Write data to the backup file using V7 format.
- -O or --stdout: Output files restored from the backup file to the standard output device.
- -p or --same-permissions: Restore files with their original permissions.
- -P or --absolute-names: Use absolute file names; do not remove the leading "/" from file names.
- -r or --append: Add files to the end of an existing backup file.
- -R or --block-number: List the block number of each information in the backup file.
-s or --same-order Restore files in the same order as they were stored in the backup.
-S or --sparse If a file contains a large number of consecutive zero bytes, store it as a sparse file.
-t or --list List the contents of the backup file.
-T<template file> or --files-from=<template file> Specify a template file containing one or more patterns for tar to unpack or create files that match the specified conditions.
-u or --update Replace only files that are newer than those in the backup.
-U or --unlink-first Unlink files before extracting them from the archive.
-v or --verbose Show the progress of the command.
-V<volume name> or --label=<volume name> Create a backup with the specified volume name.
-w or --interactive Prompt the user in case of issues.
-W or --verify Verify the integrity of the files after writing the backup.
-x or --extract or --get Extract files from the backup.
-X<template file> or --exclude-from=<template file> Specify a template file containing one or more patterns for ar to exclude files that match the specified conditions.
-z or --gzip or --ungzip Process the backup file using gzip.
-Z or --compress or --uncompress Process the backup file using compress.
-<device number><storage density> Set the peripheral device number and storage density for the backup.
--after-date=<date time> This option is equivalent to the "-N" option.
--atime-preserve Preserve the access time of the files.
--backup=<backup method> or --backup Backup files before removing them.
--checkpoint List directory names while reading the backup file.
--concatenate This option is equivalent to the "-A" option.
--confirmation This option is equivalent to the "-w" option.
--delete Delete specified files from the backup.
--exclude=<pattern> Exclude files matching the specified pattern.
--group=<group name> Set the group of files added to the device to the specified group.
--help Display online help.
--ignore-failed-read Ignore read errors and continue execution without interruption.
--new-volume-script=<script file> This option is equivalent to the "-F" option.
--newer-mtime Save only modified files.
--no-recursion Do not process subdirectories recursively.
--null Read file names from the null device.
--numeric-owner Use user and group IDs instead of names.
--owner=<user name> Set the owner of files added to the backup to the specified user.
--posix Write data to the backup file using the POSIX format.
--preserve This option is equivalent to the "-ps" option.
--preserve-order This option is equivalent to the "-A" option.
--preserve-permissions This option is equivalent to the "-p" option.
--record-size=<block count> This option is equivalent to the "-b" option.
--recursive-unlink Unlink all files in the directory before extracting the archive.
--remove-files Remove files after adding them to the backup.
--rsh-command=<command> Specify a command to replace rsh on remote hosts.
--same-owner Attempt to restore files with the same owner as the original.
--suffix=<backup suffix> Backup files before removing them.
--totals Display the size of the backup file after creation.
--use-compress-program=<command> Process the backup file using the specified command.
--version Show version information.
--volno-file=<number file> Use the volume number from the specified file instead of the default.
Examples
Compress file without packaging
# touch a.c
# tar -czvf test.tar.gz a.c // Compress a.c file to test.tar.gz
a.c
List the contents of the compressed file
# tar -tzvf test.tar.gz
-rw-r--r-- root/root 0 2010-05-24 16:51:59 a.c
Extract files
# tar -xzvf test.tar.gz
tar -xzvf test.tar.gz
a.c