Linux Commands Examples

A great documentation place for Linux commands


The GNU version of the tar archiving utility

see also : rmt


tar [

- ] A --catenate --concatenate | c --create | d --diff --compare | --delete | r --append | t --list | --test-label | u --update | x --extract --get [options] [pathname ...]

add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!



Linux / OS X tar incompatibility – tarballs created on OS X give errors when untarred in Linux

I googled for the error message and it seems like a BSD tar vs. GNU tar issue.

Install gnu tar if you can on MacOS and use that to create the tar.


Create archive.tar from files foo and bar.

tar -cf archive.tar foo bar

List all files in archive.tar verbosely.

tar -tvf archive.tar

Extract all files from archive.tar.

tar -xf archive.tar

tar -zcvf GROUPDD.tar src
tar -xzvf deps.tar.gz

How to specify level of compression when using tar -zcvf?

Instead of using the gzip flag for tar, gzip the files manually after the tar process, then you can specify the compression level for the gzip program:

tar -cvf files.tar /path/to/file0 /path/to/file1 ; gzip -9 files.tar

Or you could use:

tar cvf - /path/to/file0 /path/to/file1 | gzip -9 - > files.tar.gz

The -9 in the gzip command line tells gzip to use the maximum possible compression level (default is -6).

Edit: Fixed pipe command line based on @depesz comment.


What is the advantage of using 'tar' today?

This has been answered on Stack Overflow.

bzip and gzip work on single files, not groups of files. Plain old zip (and pkzip) operate on groups of files and have the concept of the archive built-in.

The *nix philosophy is one of small tools that do specific jobs very well and can be chained together. That's why there's two tools here that have specific tasks, and they're designed to fit well together. It also means you can use tar to group files and then you have a choice of compression tool (bzip, gzip, etc).

Many tools are distributed in source or binary form as .tar.gz or .tar.bz2 because it is a "lowest common denominator" file format: much like most Windows users have access to .zip or .rar decompressors, most Linux installations, even the most basic, will have access to at least tar and gunzip, no matter how old or pared down. Even Android firmwares have access to these tools.

New projects targeting audiences running modern distributions may very well distribute in a more modern format, such as .tar.xz (using the Xz (LZMA) compression format, which compresses better than gzip or bzip2), or .7z, which is similar to the Zip or Rar file formats in that it both compresses and specifies a layout for encapsulating multiple files into a single file.

You don't see .7z used more often for the same reason that music isn't sold from online download stores in brand new formats like Opus, or video in WebM. Compatibility with people running ancient or very basic systems.


How do I unzip a tar gz archive to a specific destination?

You have two choices:

cd /root/Desktop/folder
tar zxf /root/Documents/file.tar.gz


tar zxf file.tar.gz -C /root/Desktop/folder

Untar, ungz, gz, tar - how do you remember all the useful options?

Just type tar --help and there's your cheatsheet.


Which is more efficient - tar or zip compression? What is the difference between tar and zip?

tar only makes a single file out of multiple files, it doesn't do compression unless combined a compression program such as gzip or bzip2 (which you can call from within tar by using the -z or -j options, respectively). zip combines both the archiving and compression in one program.


How can I create a zip / tgz in Linux such that Windows has proper filenames?

As far as I know, Windows NT series (XP, Vista, others) use Unicode for storing the file names. So I'm guessing it's a problem with the Windows' archiver... which one are you using?


Undo tar file extraction mess

List the contents of the tar file like so:

tar tzf myarchive.tar

Then, delete those file names by iterating over that list:

while IFS= read -r file; do echo "$file"; done < <(tar tzf myarchive.tar.gz)

This will still just list the files that would be deleted. Replace echo with rm if you're really sure these are the ones you want to remove. And maybe make a backup to be sure.

In a second pass, remove the directories that are left over:

while IFS= read -r file; do rmdir "$file"; done < <(tar tzf myarchive.tar.gz)

This prevents directories with from being deleted if they already existed before.

Another nice trick by @glennjackman, which preserves the order of files, starting from the deepest ones. Again, remove echo when done.

tar tvf myarchive.tar | tac | xargs -d'\n' echo rm

This could then be followed by the normal rmdir cleanup.


Map "untar" to tar xvfz?

alias untar='tar -xvzf'

place in your .bashrc file to persist across logins/shell sessions.


How to use password protection with the Linux tar command?

As far as I know tar doesn't provide this service. You could use 7zip instead, which does offer encrypted archives.

What you can also do is use gpg in addition to tar:

tar cvJf myarchive.tar.xz myfiles
gpg -c myarchive.tar.xz.gpg

This will give you a password protected archive.


Tar stores and extracts files from a tape or disk archive.

The first argument to tar should be a function; either one of the letters Acdrtux, or one of the long function names. A function letter need not be prefixed with ’’-’’, and may be combined with other single-letter options. A long function name must be prefixed with --. Some options take a parameter; with the single-letter form these must be given as separate arguments. With the long form, they may be given by appending =value to the option.


The behavior of tar is controlled by the following environment variables, among others:


Backup prefix to use when extracting, if --suffix is not specified. The backup suffix defaults to ’~’ if neither is specified.


Options to prepend to those specified on the command line, separated by whitespace. Embedded backslashes may be used to escape whitespace or backslashes within an option.


Device or file to use for the archive if --file is not specified. If this environment variable is unset, use stdin or stdout instead.


function letters

Main operation mode:

-A, --catenate, --concatenate

append tar files to an archive

-c, --create

create a new archive

-d, --diff, --compare

find differences between archive and file system


delete from the archive (not on mag tapes!)

-r, --append

append files to the end of an archive

-t, --list

list the contents of an archive


test the archive volume label and exit

-u, --update

only append files newer than copy in archive

-x, --extract, --get

extract files from an archive

other options

Operation modifiers:


specify drive and density

-a, --auto-compress

use archive suffix to determine the compression program


add given FILE to the archive (useful if its name starts with a dash)


patterns match file name start


patterns match after any ’/’ (default for exclusion)


preserve access times on dumped files, either by restoring the times


do not use archive suffix to determine the compression program

-b, --blocking-factor BLOCKS

BLOCKS x 512 bytes per record

-B, --read-full-records

reblock as we read (for 4.2BSD pipes)


backup before removal, choose version CONTROL

-C, --directory DIR

change to directory DIR


check device numbers when creating incremental archives (default)


do not check device numbers when creating incremental archives


display progress messages every NUMBERth record (default 10)


execute ACTION on each checkpoint


delay setting modification times and permissions of extracted


cancel the effect of --delay-directory-restore option


exclude files, given as a PATTERN


exclude backup and lock files


exclude contents of directories containing CACHEDIR.TAG,


exclude directories containing CACHEDIR.TAG


exclude everything under directories containing CACHEDIR.TAG


exclude contents of directories containing FILE, except


exclude directories containing FILE


exclude everything under directories containing FILE


exclude version control system directories

-f, --file ARCHIVE

use archive file or device ARCHIVE

-F, --info-script, --new-volume-script NAME

run script at end of each tape (implies -M)


archive file is local even if it has a colon


print file time to its full resolution

-g, --listed-incremental FILE

handle new GNU-format incremental backup

-G, --incremental

handle old GNU-format incremental backup


force NAME as group for added files

-h, --dereference

follow symlinks; archive and dump the files they point to

-H, --format FORMAT

create archive of the given formatFORMAT is one of the following:


GNU tar 1.13.x format


GNU format as per tar <= 1.12


POSIX 1003.1-2001 (pax) format


same as pax


POSIX 1003.1-1988 (ustar) format


old V7 tar format


follow hard links; archive and dump the files they refer to

-i, --ignore-zeros

ignore zeroed blocks in archive (means EOF)

-I, --use-compress-program PROG

filter through PROG (must accept -d)


ignore case


case sensitive matching (default)


ignore exit codes of children


treat non-zero exit codes of children as error


do not exit with nonzero on unreadable files


send verbose output to FILE

-j, --bzip2

-J, --xz

-k, --keep-old-files

don’t replace existing files when extracting

-K, --starting-file MEMBER-NAME

begin at member MEMBER-NAME in the archive


don’t replace existing files that are newer than their archive copies

-l, --check-links

print a message if not all links are dumped

-L, --tape-length NUMBER

change tape after writing NUMBER x 1024 bytes


dump level for created listed-incremental archive




-m, --touch

don’t extract file modified time

-M, --multi-volume

create/list/extract multi-volume archive


force (symbolic) mode CHANGES for added files


set mtime for added files from DATE-OR-FILE

-n, --seek

archive is seekable

-N, --newer, --after-date DATE-OR-FILE

only store files newer than DATE-OR-FILE


compare date and time when data changed only


-T reads null-terminated names, disable -C


disable the effect of the previous --null option


always use numbers for user/group names

-O, --to-stdout

extract files to standard output


process only the NUMBERth occurrence of each file in the archive;

--old-archive, --portability

same as --format=v7


stay in local file system when creating archive


overwrite existing files when extracting


overwrite metadata of existing directories when extracting (default)


preserve metadata of existing directories


force NAME as owner for added files

-p, --preserve-permissions, --same-permissions

extract information about file permissions (default for superuser)

-P, --absolute-names

don’t strip leading ’/’s from file names


control pax keywords


same as --format=posix


same as both -p and -s


additionally quote characters from STRING


disable quoting for characters from STRING


set name quoting style; see below for valid STYLE values

-R, --block-number

show block number within archive with each message


NUMBER of bytes per record, multiple of 512


recurse into directories (default)


avoid descending automatically in directories


empty hierarchies prior to extracting directory


remove files after adding them to the archive


disable use of some potentially harmful options


use given rmt COMMAND instead of rmt


use remote COMMAND instead of rsh

-s, --preserve-order, --same-order

sort names to extract to match archive

-S, --sparse

handle sparse files efficiently


try extracting files with the same ownership as exists in the archive (default for superuser)


extract files as yourself (default for ordinary users)


apply the user’s umask when extracting permissions from the archive (default for ordinary users)


archive is not seekable


show tar defaults


when listing or extracting, list each directory that does not match search criteria

--show-transformed-names, --show-stored-names

show file or archive names after transformation


set version of the sparse format to use (implies --sparse)


strip NUMBER leading components from file names on extraction


backup before removal, override usual suffix (’~’ unless overridden by environment variable SIMPLE_BACKUP_SUFFIX)

-T, --files-from FILE

get names to extract or create from FILE


pipe extracted files to another program


print total bytes after processing the archive;

--transform, --xform EXPRESSION

use sed replace EXPRESSION to transform file names

-U, --unlink-first

remove each file prior to extracting over it


unquote filenames read with -T (default)


do not unquote filenames read with -T


print file modification times in UTC

-v, --verbose

verbosely list files processed

-V, --label TEXT

create archive with volume name TEXT; at list/extract time, use TEXT as a globbing pattern for volume name


use/update the volume number in FILE

-w, --interactive, --confirmation

ask for confirmation for every action

-W, --verify

attempt to verify the archive after writing it


warning control


use wildcards (default for exclusion)


wildcards match ’/’ (default for exclusion)


wildcards do not match ’/’


verbatim string matching

-X, --exclude-from FILE

exclude patterns listed in FILE

-z, --gzip, --gunzip --ungzip

-Z, --compress, --uncompress


The GNU folks, in general, abhor man pages, and create info documents instead. Unfortunately, the info document describing tar is licensed under the GFDL with invariant cover texts, which makes it impossible to include any text from that document in this man page. Most of the text in this document was automatically extracted from the usage text in the source. It may not completely describe all features of the program.

Apr 4, 2013


The tar command appeared in Version 7 AT&T UNIX.

see also

tar, symlink, rmt

How can this site be more helpful to YOU ?

give  feedback