Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux tar Command

Linux tar Command

Linux Command Reference Linux Command Reference

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...]

options parameters

Basic operation options

File Selection and Exclusion

Compression and Decompression Options

Output and Interaction Options

File and Permission Options

Archive Management Options

Filesystem and Device Options

Other useful options

Help and Version 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

** 2. Extract an archive: ** Extract the archive named archive.tar, restoring the files and directories it contains.

tar -xvf archive.tar

** 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

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

tar -tvf archive.tar

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

tar -rvf archive.tar newfile

** 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

** 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

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 Linux Command Reference