Linux tar Command
The Linux tar (short for "tape archive") command is used to back up files.
tar is a powerful command-line tool in Linux and Unix systems for archiving files and directories.
The name tar comes from "tape archive"; it was originally used to package files onto tape devices, but is now widely used to package and compress files in the filesystem.
tar is typically used to package multiple files and directories into a single archive file called a "tarball" (usually with a .tar extension).
tar itself does not compress files, but it can be combined with compression tools (such as gzip or bzip2) to create compressed archives (such as .tar.gz or .tar.bz2).
Syntax
tar [options] -f archive.tar [files...]
-f archive.tar: Specify the name of the archive file.[files...]: The files and directories to package.
options parameters
Basic operation options
-c: Create a new archive file.-x: Extract an archive file.-t: List the contents of an archive file.-r: Append files to an existing archive file.-u: Append only files that are newer than those already in the archive.-d: Find differences between the archive and the filesystem that are out of sync.-A: Append a.tarfile to another.tarfile.
File Selection and Exclusion
-f: Specify the name of the archive file (must be placed last in the options list).-C: Change to the specified directory before operating.--exclude=: Exclude files matching the specified pattern.--exclude-from=: Read exclusion patterns from the specified file.--exclude-caches: Exclude cache files in directories.--exclude-backups: Exclude files ending with~backup files.--exclude-vcs: Exclude files generated by version control systems (such as.git,.svnetc.).
Compression and Decompression Options
-z: When usinggzipCompress the archive file.-j: When usingbzip2Compress the archive file.-J: When usingxzCompress the archive file.--lzip: When usinglzipCompress the archive file.--lzma: When usinglzmaCompress the archive file.--lzop: When usinglzopCompress the archive file.--zstd: When usingzstdCompress the archive file.-a: Automatically select the compression method (based on the archive extension, e.g..tar.gz,.tar.bz2etc.).-I: Use the specified compression program to compress or decompress.
Output and Interaction Options
-v: Display detailed operation progress (verbose).--progress: Display a progress bar (when used with-vtogether).-wor--interactive: Ask the user for confirmation before each operation.--checkpoint: Display a checkpoint after processing each file.--checkpoint-action=: Perform a specified action at a checkpoint, such asecho,dot, etc.--totals: Display the total number of bytes processed after the operation.--verbose: Display detailed processing information.--quiet: Output as little information as possible.
File and Permission Options
-p: Preserve the original file permissions (when extracting).--same-owner: Try to set extracted files to their original owner (requires superuser privileges).--no-same-owner: Do not set file ownership.--same-permissions: Preserve the original file permissions (same as-p).--no-same-permissions: Do not preserve original permissions; use the current user's umask to set permissions.-m: When extracting, do not restore the file modification time; use the current time instead.
Archive Management Options
-kor--keep-old-files: When extracting, keep existing files without overwriting.--overwrite: When extracting, force overwrite of existing files.--remove-files: Delete the original files after successful archiving.--delete: Delete specified files from the archive (only forgnu tar).--keep-newer-files: When extracting, keep files that are newer than those in the archive.--listed-incremental=: Create incremental backups or restore from incremental backups.
Filesystem and Device Options
-L: Split archives larger thanNbytes (for tape drives).--tape-length=: Specify the tape length (for tape drives).--multi-volume: Create or restore multi-volume archive files.-M: Together with--multi-volumetogether to handle multi-volume archives.--use-compress-program=: Use the specified compression program.
Other useful options
--transform=: Rename files in the archive.--strip-components=: Strip the specified number of path components when extracting.--ignore-failed-read: Ignore read errors and continue the operation.--occurrence=: Select thenumberoccurrence of a file in the archive.-S: Handle sparse files (only archive blocks actually in use).--no-recursion: Do not recurse into directories.-hor--dereference: Archive the file a symbolic link points to rather than the link itself.
Help and Version Information
--help: Display help information.--version: Displaytarversion information.
Example
** 1. Create an archive: ** Package the files file1, file2, and directory into an archive named archive.tar.
tar -cvf archive.tar file1 file2 directory
-c: Create a new archive file-v: Display verbose output, listing the files added to the archive-f: Specify the name of the archive file
** 2. Extract an archive: ** Extract the archive named archive.tar, restoring the files and directories it contains.
tar -xvf archive.tar
-x: Extract the archive file-v: Display verbose output, listing the extracted files-f: Specify the name of the archive file to extract
** 3. Compress an archive: ** Package the directory named directory into an archive, then compress it with gzip to produce a file named archive.tar.gz.
tar -czvf archive.tar.gz directory
-c: Create a new archive file-z: Compress the archive with gzip-v: Display verbose output, listing the files added to the archive-f: Specify the name of the archive file
** 4. List archive contents: ** List all files and directories contained in the archive named archive.tar.
tar -tvf archive.tar
-t: List the contents of the archive-v: Display verbose output, listing all files and directories in the archive-f: Specify the name of the archive whose contents to list
** 5. Append to an existing archive: ** Add the file named newfile to the existing archive named archive.tar.
tar -rvf archive.tar newfile
-r: Append files to an existing archive-v: Display verbose output, listing the files added to the archive-f: Specify the name of the existing archive file
** 6. Create a gzip-compressed archive: ** Package all files and subdirectories under the directory directory and compress with gzip to create an archive named archive.tar.gz.
tar -zcvf archive.tar.gz directory
-z: Indicates compression with gzip.-c: Indicates creation of a new archive file.-v: Indicates verbose output, listing files added to the archive.-f archive.tar.gz: Specify the archive file name asarchive.tar.gz.
** 7. Extract a gzip-compressed archive: ** Extract the file example.tar.gz and restore the files and directories it contains in the current directory.
tar -zxvf example.tar.gz
-z: Indicates decompression of the archive with gzip.-x: Indicates the extraction operation.-v: Indicates verbose output, listing the extracted files.-f example.tar.gz: Specify the name of the archive file to extract asexample.tar.gz.
Specifying the Compression Format
tar can be combined with different compression programs to create and extract compressed archives.
z : Compress with gzip.
tar -czvf archive.tar.gz directory
tar -xzvf archive.tar.gz
j: Compress with bzip2.
tar -cjvf archive.tar.bz2 directory
tar -xjvf archive.tar.bz2
J: Compress with xz.
tar -cJvf archive.tar.xz directory
tar -xJvf archive.tar.xz
Linux Command Reference