Skip to content
Search

Tar

lastmod 2026-02-06created 2014-08-0621 edits

In computing, tar refers both to a file format designed for tape archives and to the program used to handle files in that format. The file format was created in the early days of Unix and was standardized by POSIX.1-1988 and POSIX.1-2001.

It was originally developed for tape backups, to write directly to sequential I/O devices, but today it is mainly used to bundle many files into one large file for distribution or archiving while preserving the directory structure and file attributes.

Flags

  • -c: When compressing.
  • -x: When extracting.
  • -t: When checking the contents of an archive.
  • -f: When using an archive file.
  • -p: Preserve permissions when compressing and extracting.
  • -v: Show progress when compressing and extracting.
  • -z: Use gzip.
  • -j: Use bzip.

List the contents

List the contents of a tar file
$ tar -tvf file.tar
List the contents of a tar.gz file
$ tar -ztvf file.tar.gz
List the contents of a tar.bz2 file
$ tar -jtvf file.tar.bz2

Specifying the output directory path

Use the -C option to specify the output directory path.

tar -xf file.tar.gz -C $HOME/output

Splitting and joining files

Use the split and cat commands.

# Split tar file into 4G chunks / tar 파일을 4G 단위로 분할
split -b 4G archive.tar archive.tar.part_

# Merge back / 다시 합치기
cat archive.tar.part_* > archive.tar

Adding split -d makes the suffixes numeric (part_00, part_01, ...).

Receiving a pipe from CURL

curl -L https://github.com/slackhq/nebula/releases/download/v1.9.1/nebula-linux-amd64.tar.gz | tar xzf - -C ~/bin

Things to note:

  • A pipe needs -.
  • Changing the output location needs -C {dir}, which must come after - (i.e. argument order matters)
  • sudo cannot be used in front of curl. So you have to enter first with something like sudo -s and then start.

Absolute path

If you don't want absolute paths to be stripped, which happens automatically when compressing, use the --absolute-names or -P option.

$ tar fczP bkup.tar.gz /home/foo/
$ tar fcz bkup.tar.gz --absolute-names /home/foo

Remove first-depth directory

If you want to extract after stripping the directory path of the archived files, use the --strip-components=NUMBER option.

 --strip-components=NUMBER
       strip NUMBER leading components from file names on extraction

Your command would be:

tar xfz /var/www/site.gz --strip-components=2 -C /tmp

See also

Favorite site

author Zer0 sitemap