[Linux] Create, Compress, and Extract Archives with the tar Command

目次

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.

ModeDescription
-cCreates a new archive (create).
-xExtracts files from an archive (extract).
-tLists the contents of an archive (list).
-rAppends files to the end of an uncompressed archive (append).
-uUpdates the archive with files that are newer than those already inside (update).
-AAppends another tar file to an existing archive (catenate).
-dCompares the difference between the archive and the file system (diff).
--deleteDeletes specified files from an uncompressed archive.

Main Options

OptionDescription
-f <file>Specifies the archive filename (file). This is required.
-vDisplays the names of files being processed (verbose).
-zHandles archives using gzip compression (.tar.gz).
-jHandles archives using bzip2 compression (.tar.bz2).
-JHandles archives using xz compression (.tar.xz).
-C <dir>Changes to a specific directory before performing the action.
-pPreserves file permissions during extraction.
--exclude=<p>Excludes files that match the specified pattern.
--remove-filesDeletes the original source files after creating the archive.
-T <file>Reads a list of target files from a text file.
--nullUsed with -T to indicate the list is null-terminated.
-OWrites extracted files to standard output.
-hFollows symbolic links and archives the actual target (dereference).
--backupCreates 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 -f option must be immediately followed by the filename. It is standard practice to place f at the end of the option string, such as -czvf.
  • Destination Folder: Use -C /tmp to extract files to a specific directory instead of your current location.

Important Notes

  • No Appending to Compressed Files: Modes like -r (append) or --delete do not work on compressed files (like .tar.gz). You must extract the files and create a new archive.
  • Absolute Paths: For security, tar removes 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.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次