Linux tar command

Linux 命令大全Linux command collection

The Linux tar (full English name: tape archive) command is used to back up files.

tar is a powerful command-line tool for archiving files and directories in Linux and Unix systems.

The name tar comes from "tape archive". It was originally used to package files to tape devices, but now it is widely used for packaging and compressing files in file systems.

tar is commonly used to package multiple files and directories into a single archive file called a "tarball" (usually with the .tar extension).

tar itself does not compress files, but it can be combined with compression tools (such as gzip or bzip2) to create compressed archive files (such as .tar.gz or .tar.bz2).

Syntax

tar [options] -f archive.tar [files...]
  • -f archive.tar: Specifies the name of the archive file.
  • [files...]: The files and directories to be packaged.

options parameters

Basic operation options

  • -c: Create a new archive file.
  • -x: Extract archive files.
  • -t: List the contents of the archive file.
  • -r: Append files to an existing archive file.
  • -u: Only append files that are newer than those already in the archive.
  • -d: Find differences between the archive and the file system.
  • -A: Add a.tarfile to another.tarfile.

File selection and exclusion

  • -f <file>: Specify the name of the archive file (must be placed at the end of the option list).
  • -C <directory>: Change to the specified directory for operation.
  • --exclude=<pattern>: Exclude files matching the specified pattern.
  • --exclude-from=<file>: Read exclusion patterns from the specified file.
  • --exclude-caches: Exclude cache files in directories.
  • --exclude-backups: Exclude files ending in~backup files.
  • --exclude-vcs: Exclude files generated by version control systems (such as.git、.svnetc.).

Compression and decompression options

  • -z: Usegzipcompress the archive file.
  • -j: Usebzip2compress the archive file.
  • -J: Usexzcompress the archive file.
  • --lzip: Uselzipcompress the archive file.
  • --lzma: Uselzmacompress the archive file.
  • --lzop: Uselzopcompress the archive file.
  • --zstd: Usezstdcompress the archive file.
  • -a: Automatically choose compression method (based on the archive file extension, such as.tar.gz、.tar.bz2etc.).
  • -I <command>: Use the specified compression program to compress or decompress.

Output and interaction options

  • -v: Display detailed operation process (verbose).
  • --progress: Display progress bar (when used with-vtogether).
  • -wor--interactive: Ask user for confirmation before each operation.
  • --checkpoint: Display a checkpoint after processing each file.
  • --checkpoint-action=<action>: Execute the specified action at the checkpoint, such asecho、dotetc.
  • --totals: Display the total number of bytes processed after the operation ends.
  • --verbose: Display detailed information about the processing.
  • --quiet: Output as little information as possible.

File and permission related options

  • -p: Preserve the original permissions of files (when extracting).
  • --same-owner: Attempt to set the extracted files to the original owner (requires superuser privileges).
  • --no-same-owner: Do not set file owner.
  • --same-permissions: Preserve the original permissions (same as-psame).
  • --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 and do not overwrite.
  • --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=<file>: Create an incremental backup or restore from an incremental backup.

File system and device options

  • -L <N>: Split archive files larger thanNbytes (for tape drives).
  • --tape-length=<number>: Specify tape length (for tape drives).
  • --multi-volume: Create or restore multi-volume archive files.
  • -M: Used with--multi-volumetogether, to handle multi-volume archive files.
  • --use-compress-program=<prog>: Use the specified compression program.

Other practical options

  • --transform=<expression>: Rename files in the archive.
  • --strip-components=<number>: Strip the specified number of path components when extracting.
  • --ignore-failed-read: Ignore read errors and continue the operation.
  • --occurrence=<number>: In the archive, select thenumberoccurrence of the file.
  • -S: Handle sparse files (archive only the blocks actually used).
  • --no-recursion: Do not recursively enter directories.
  • -hor--dereference: Archive the files pointed to by symbolic links, not the links themselves.

Help and version information

  • --help: Display help information.
  • --version: Displaytarversion information.

Examples

1. Create an archive file:Package the files file1, file2, and directory into an archive file named archive.tar.

tar -cvf archive.tar file1 file2 directory
  • -c: Create a new archive file
  • -v: Display detailed output, listing files added to the archive
  • -f: Specify the name of the archive file

2. Extract the archive file:Extract the archive file named archive.tar and restore the files and directories it contains.

tar -xvf archive.tar
  • -x: Extract the archive file
  • -v: Display detailed output, listing the extracted files
  • -f: Specify the name of the archive file to extract

3. Compress the archive file:Package the directory named directory into an archive file, then use gzip to compress it, generating a file named archive.tar.gz.

tar -czvf archive.tar.gz directory
  • -c: Create a new archive file
  • -z: Use gzip to compress the archive file
  • -v: Display detailed output, listing files added to the archive
  • -f: Specify the name of the archive file

4. List the contents of the archive file:List all files and directories contained in the archive file named archive.tar.

tar -tvf archive.tar
  • -t: List the contents of the archive file
  • -v: Display detailed output, listing all files and directories in the archive file
  • -f: Specify the name of the archive file whose contents are to be listed

5. Append files to an existing archive:Add the file named newfile to the existing archive file named archive.tar.

tar -rvf archive.tar newfile
  • -r: Append files to an existing archive
  • -v: Display detailed output, listing files added to the archive
  • -f: Specify the name of the existing archive file

6. Create a gzip-compressed archive file:Package all files and subdirectories under the directory directory, and use gzip to compress, generating an archive file named archive.tar.gz.

tar -zcvf archive.tar.gz directory
  • -z: Indicates that gzip should be used for compression.
  • -c: Indicates creating a new archive file.
  • -v: Indicates detailed output, listing files added to the archive.
  • -f archive.tar.gz: Specify the archive file name asarchive.tar.gz。

7. Extract an archive file that has already been gzip-compressed:Extract the example.tar.gz file and restore the files and directories it contains in the current directory.

tar -zxvf example.tar.gz
  • -z: Indicates that gzip should be used to extract the archive file.
  • -x: Indicates extraction operation.
  • -v: Indicates detailed output, listing the extracted files.
  • -f example.tar.gz: Specify the name of the archive file to extract asexample.tar.gz。

Specify compression format

tar can be combined with different compression programs to create and extract compressed archive files.

z: Use gzip compression.

tar -czvf archive.tar.gz directory
tar -xzvf archive.tar.gz

j: Use bzip2 compression.

tar -cjvf archive.tar.bz2 directory
tar -xjvf archive.tar.bz2

J: Use xz compression.

tar -cJvf archive.tar.xz directory
tar -xJvf archive.tar.xz

Linux 命令大全Linux command collection

Other extensions