Overview
The tar command is used to bundle multiple files or directories into a single file called an “archive,” or to extract files from such an archive. The name comes from “Tape ARchive,” but today it is the standard tool in Linux for creating backups and distributing source code (tarballs). It is also frequently used with compression tools like gzip or bzip2 to reduce file size.
Specifications (Arguments and Options)
Syntax
tar [mode] [options] archive_name [target_files...]
Operational Modes (Functions)
You must specify exactly one of these modes.
| Mode | Description |
-c | Creates a new archive (create). |
-x | Extracts files from an archive (extract). |
-t | Lists the contents of an archive (list). |
-r | Appends files to the end of an uncompressed archive (append). |
-u | Updates the archive with files that are newer than those already inside (update). |
-A | Appends another tar file to an existing archive (catenate). |
-d | Compares the difference between the archive and the file system (diff). |
--delete | Deletes specified files from an uncompressed archive. |
Main Options
| Option | Description |
-f <file> | Specifies the archive filename (file). This is required. |
-v | Displays the names of files being processed (verbose). |
-z | Handles archives using gzip compression (.tar.gz). |
-j | Handles archives using bzip2 compression (.tar.bz2). |
-J | Handles archives using xz compression (.tar.xz). |
-C <dir> | Changes to a specific directory before performing the action. |
-p | Preserves file permissions during extraction. |
--exclude=<p> | Excludes files that match the specified pattern. |
--remove-files | Deletes the original source files after creating the archive. |
-T <file> | Reads a list of target files from a text file. |
--null | Used with -T to indicate the list is null-terminated. |
-O | Writes extracted files to standard output. |
-h | Follows symbolic links and archives the actual target (dereference). |
--backup | Creates a backup of existing files when overwriting. |
--suffix=<s> | Specifies the suffix for backup files. |
Basic Usage
The most common use is creating a gzip-compressed archive of a directory. We combine the options -c (create), -z (gzip), -v (verbose), and -f (file).
Command
# Bundle the "logs" directory into logs_backup.tar.gz
tar -czvf logs_backup.tar.gz logs/
Execution Result
logs/
logs/access.log
logs/error.log
Practical Commands
Create Compressed Archives (gzip, bzip2, xz)
Choose the format based on whether you want speed or a smaller file size.
# gzip: Common and fast
tar -czvf archive.tar.gz ./target_dir
# bzip2: High compression, slower than gzip
tar -cjvf archive.tar.bz2 ./target_dir
# xz: Best compression, slow processing
tar -cJvf archive.tar.xz ./target_dir
Extract Compressed Archives
Use -x to unpack. Modern versions of tar automatically detect the compression format from the file extension even if you don’t specify -z or -j.
# Extract an archive
tar -xvf archive.tar.gz
Use Standard Input and Output
You can use pipelines to handle archives. Using -f - tells the command to use standard I/O.
# Bundle files and send them to standard output (then save via redirect)
tar -cvf - file1.txt file2.txt > manual_archive.tar
# Read tar data from standard input and list its contents
cat manual_archive.tar | tar -tvf -
Add or Combine Files (Uncompressed Only)
You can add files to or combine archives if they are not compressed.
# Add a new file to an existing archive
tar -rvf archive.tar new_file.txt
# Merge archive2.tar into archive1.tar
tar -Avf archive1.tar archive2.tar
Delete Files from an Archive (Uncompressed Only)
# Delete a specific file inside archive.tar
tar --delete -f archive.tar old_config.conf
Keep Backups of Existing Archives
When creating an archive, you can avoid overwriting old ones by adding a suffix like .old.
# Set the backup suffix (can also be an environment variable)
export SIMPLE_BACKUP_SUFFIX=".old"
# Run with the --backup option
tar -cJvf --backup archive.tar.xz ./src/
Customization Points
- Option Order: The
-foption must be immediately followed by the filename. It is standard practice to placefat the end of the option string, such as-czvf. - Destination Folder: Use
-C /tmpto extract files to a specific directory instead of your current location.
Important Notes
- No Appending to Compressed Files: Modes like
-r(append) or--deletedo not work on compressed files (like.tar.gz). You must extract the files and create a new archive. - Absolute Paths: For security,
tarremoves the leading/from absolute paths and treats them as relative. This prevents overwriting system files during extraction. - Overwriting: When extracting (
-x), if a file with the same name exists in the directory, it will be overwritten without warning.
Applications
Safely Archive Results from the find Command
When archiving many files found with find, use the --null and -T options to handle filenames containing spaces correctly.
# Find .log files and archive them safely using null delimiters
find ./var/log -name "*.log" -print0 | tar -czvf logs_collection.tar.gz --null -T -
Summary
The tar command is a fundamental tool for file management in Linux. It is used for everything from backups to server data transfers and source code distribution. By mastering the basic “c” (create), “x” (extract), and “t” (list) modes along with compression options like z, j, and J, you can handle almost any daily file task.
