What it is
tar is a command-line utility for creating, listing, and extracting archive files, often referred to as "tarballs." It’s widely used for bundling multiple files and directories into a single file and for compressing them.
Installation
Linux
tar is usually pre-installed on most Linux distributions. If not:
sudo apt update && sudo apt install tar # For Debian/Ubuntu
sudo yum install tar # For CentOS/RHEL
sudo dnf install tar # For Fedora
macOS
tar is pre-installed on macOS.
Windows
tar is available in recent versions of Windows 10 and Windows 11 through the Windows Subsystem for Linux (WSL) or as a built-in command.
# Open PowerShell or Command Prompt as Administrator
# To install via winget (if available)
winget install gnu.tar
Alternatively, you can download a pre-compiled binary from sources like GnuWin32 or use WSL.
Core Concepts
- Archive: A single file that contains one or more other files and directories, preserving their structure and metadata.
- Compression:
taritself does not compress files. It’s common to pipe the output oftarto a compression utility (likegzip,bzip2, orxz) or usetar’s built-in support for these compression algorithms via specific flags. - Modes:
taroperates in different modes:- Create (
-c): Make a new archive. - Extract (
-x): Unpack an archive. - List (
-t): Show the contents of an archive. - Update (
-u): Add files to an archive if they are newer than existing files. - Compare (
-d): Find differences between an archive and files.
- Create (
Commands / Usage
Creating Archives
-
Create a simple archive:
tar -cvf backup.tar /home/user/documentsCreate (
-c) a verbose (-v) archive file (-f) namedbackup.tarcontaining the/home/user/documentsdirectory. -
Create a gzipped archive:
tar -czvf backup.tar.gz /home/user/documentsCreate (
-c) a verbose (-v) gzip-compressed (-z) archive file (-f) namedbackup.tar.gz. -
Create a bzip2 archive:
tar -cjvf backup.tar.bz2 /home/user/documentsCreate (
-c) a verbose (-v) bzip2-compressed (-j) archive file (-f) namedbackup.tar.bz2. -
Create an xz archive:
tar -cJvf backup.tar.xz /home/user/documentsCreate (
-c) a verbose (-v) xz-compressed (-J) archive file (-f) namedbackup.tar.xz. -
Create an archive from a list of files:
tar -cvf files.tar file1.txt file2.jpg dir1/Create (
-c) a verbose (-v) archive file (-f) namedfiles.tarcontainingfile1.txt,file2.jpg, and thedir1/directory. -
Create an archive and exclude specific files/directories:
tar -czvf project.tar.gz --exclude='*.log' --exclude='tmp/' /path/to/projectCreate (
-c) a verbose (-v) gzip-compressed (-z) archive (-f) namedproject.tar.gzfrom/path/to/project, excluding all.logfiles and thetmp/directory. -
Create an archive from standard input:
tar -czf - /path/to/data | ssh user@remote 'cat > /backup/data.tar.gz'Create (
-c) a gzip-compressed (-z) archive (-f) to standard output (-) from/path/to/data, then pipe it to a remote server to save asdata.tar.gz.
Extracting Archives
-
Extract a simple archive:
tar -xvf backup.tarExtract (
-x) verbosely (-v) from the archive file (-f) namedbackup.tar. Files are extracted into the current directory. -
Extract a gzipped archive:
tar -xzvf backup.tar.gzExtract (
-x) verbosely (-v) from the gzip-compressed (-z) archive file (-f) namedbackup.tar.gz. -
Extract a bzip2 archive:
tar -xjvf backup.tar.bz2Extract (
-x) verbosely (-v) from the bzip2-compressed (-j) archive file (-f) namedbackup.tar.bz2. -
Extract an xz archive:
tar -xJvf backup.tar.xzExtract (
-x) verbosely (-v) from the xz-compressed (-J) archive file (-f) namedbackup.tar.xz. -
Extract to a specific directory:
tar -xzvf backup.tar.gz -C /path/to/extract/toExtract (
-x) verbosely (-v) frombackup.tar.gzinto the directory specified by-C. -
Extract specific files from an archive:
tar -xvf backup.tar home/user/documents/report.docxExtract (
-x) verbosely (-v) frombackup.tar, but only the filehome/user/documents/report.docx(path must match archive’s internal path). -
Extract from standard input:
ssh user@remote 'tar -czf - /data/backup' | tar -xzvf - -C /local/restore/pathReceive a gzip-compressed tar stream (
-z) from standard input (-) via SSH, and extract (-x) verbosely (-v) into/local/restore/path.
Listing Archive Contents
-
List contents of a simple archive:
tar -tvf backup.tarList (
-t) verbosely (-v) the contents of the archive file (-f) namedbackup.tar. -
List contents of a gzipped archive:
tar -tzvf backup.tar.gzList (
-t) verbosely (-v) the contents of the gzip-compressed (-z) archive file (-f) namedbackup.tar.gz. -
List contents of a bzip2 archive:
tar -tjvf backup.tar.bz2List (
-t) verbosely (-v) the contents of the bzip2-compressed (-j) archive file (-f) namedbackup.tar.bz2. -
List contents of an xz archive:
tar -tJvf backup.tar.xzList (
-t) verbosely (-v) the contents of the xz-compressed (-J) archive file (-f) namedbackup.tar.xz.
Updating Archives
-
Add a new file to an existing archive:
tar -uvf backup.tar new_file.txtUpdate (
-u) verbosely (-v) the archive file (-f)backup.tarby addingnew_file.txt. Ifnew_file.txtalready exists inbackup.tar, it will be replaced only ifnew_file.txton disk is newer. -
Add a new directory to an existing archive:
tar -uvzf backup.tar.gz added_directory/Update (
-u) verbosely (-v) the gzip-compressed (-z) archive file (-f)backup.tar.gzby addingadded_directory/.
Other Operations
-
Compare archive contents with files on disk:
tar -d -f backup.tar -C /home/user/documentsCompare (
-d) the archive file (-f)backup.tarwith the contents of/home/user/documents. Differences will be printed. -
Update archive members (only if newer):
tar -uvf myarchive.tar file_to_update.txt another_file.logUpdate (
-u) verbosely (-v) the archive file (-f)myarchive.tar. For each specified file (file_to_update.txt,another_file.log), it’s added to the archive if it doesn’t exist or if the file on disk is newer than the one in the archive.
Common Patterns
-
Backup current directory to a remote server:
tar -czvf - . | ssh user@remote_host 'cat > /backups/my_project_$(date +%Y%m%d).tar.gz'Create a compressed tarball (
-czvf -) of the current directory (.) and pipe it (|) tossh. On the remote host,catreceives the stream and redirects it (>) into a timestamped file. -
Restore a backup from a remote server:
ssh user@remote_host 'tar -czvf - /path/to/remote/backup.tar.gz' | tar -xzvf - -C /local/restore/pathOn the remote host, create a compressed tarball (
-czvf -) of a backup file and pipe it tossh. On the local machine,tarreceives the stream (-) and extracts (-xzvf -) it into/local/restore/path. -
Create a tarball without compression:
tar -cvf archive.tar file1.txt dir1/Useful when you just need to bundle files, not compress them, perhaps for later processing or when compression is handled by another tool.
-
Extracting multiple tarballs sequentially:
for f in *.tar.gz; do tar -xzvf "$f" -C extracted_data/; doneIterate through all
.tar.gzfiles in the current directory and extract each one into theextracted_data/subdirectory. -
Handling very large files (avoiding memory issues):
taris generally efficient with disk I/O and doesn’t load entire archives into memory unless specifically instructed (which is rare). The primary concern for large files is disk space and transfer time. Using compression (-z,-j,-J) is standard practice. -
Creating a tarball from a list of files in a file:
tar -cvf archive.tar --files-from=list_of_files.txtCreate an archive (
-cvf) using the file paths listed inlist_of_files.txt. Each line inlist_of_files.txtshould be a path to a file or directory. -
Extracting a tarball with absolute paths (use with caution): By default,
tarusually strips leading slashes from absolute paths in archives for security. To preserve them (potentially dangerous if the archive is untrusted):tar -xvf --absolute-names archive.tar
Gotchas
- Path Handling:
tarby default strips leading/from filenames when creating archives. This prevents accidentally overwriting system files during extraction. If you need to preserve absolute paths (e.g., for system backups where you intend to restore to the exact same location), use the--absolute-namesor-Pflag, but be extremely cautious. - Compression Flags: Remember that
taritself doesn’t compress. You need to use specific flags (-zfor gzip,-jfor bzip2,-Jfor xz) during creation and extraction. If you try to extract a.tar.gzfile withtar -xvf, it will fail. You must usetar -xzvf. Moderntarversions often auto-detect compression, but it’s best practice to specify the flag. --files-fromvs.-T: The--files-fromoption reads filenames from a file. The-Toption is a synonym for--files-from.- Order of Flags: While many
tarimplementations are lenient, the traditional order is-cor-xor-t, followed by-v,-f, and then compression flags like-z. For example,tar -xvfis standard;tar -vxvfmight work but is less conventional. The-fflag usually needs to be the last option before the archive filename. - Extracting to Existing Directories: When extracting,
tarwill create directories as needed. If a file already exists in the destination,tarmight overwrite it or refuse, depending on the version and flags. Be mindful of where you are extracting. - Permissions:
tarpreserves file permissions, ownership, and timestamps by default. When extracting, the user runningtarmust have appropriate permissions to create files and directories in the target location. Restoring ownership might require runningtaras root. - Wildcards: Wildcards (
*,?) used on the command line to specify files for creation are expanded by the shell beforetarsees them. This meanstar -cvf archive.tar *.txtwill add only the.txtfiles that exist at the time the command is run. Using--files-fromis often more robust for managing lists of files. Wildcards used with--excludeare interpreted bytar.