Search notes:

Shell command: tar

tar stands for tape-archiver and is a utility to create such archives and then adding, updating or removing files from it (see the example tar archive lifecycle).
Often, tar is used in combination with gzip so that the tarred file can be reduced in size (the resulting file is then a .tar.gz or .tgz file).
A peculiarity of the tar command is that it doesn't introduce command line options with a hyphen or dash.

Examples

Lifecycle

The following example
  • creates a tar file, then
  • updates files within the tar file, then
  • lists the file names within the tar file, then
  • extracts a file from the tar file, then
  • updates a file within the tar file again, then
  • deletes a file within the tar file, then
  • extracts the tar file's entire content to a different directory.
rm -f my.tar *.txt

printf "one\ntwo\nthree\n" > numbers.txt
printf "foo\nbar\nbaz\n"   > fooBarBaz.txt

# c: Create tar file
tar cf my.tar *.txt

# Sleep a second for tar to recognize the updated file by timestamp
sleep 1
echo four >> numbers.txt

# u: Update newer files
tar uf my.tar *.txt

# t: list contents of tar
#    Note how update has appended numbers.txt to the
#    end (so, in fact, it's contained twice)
tar tvf my.tar


rm *.txt

# x: Extract a file
tar xf my.tar numbers.txt
cat numbers.txt

# u: update (add) another file
echo abc > letters.txt
tar uf my.tar letters.txt

# delete a file
tar --delete -f my.tar numbers.txt
echo ''
tar tvf my.tar

# -C to extract to different directory
rm *.txt
tar xf my.tar -C /tmp letters.txt
cat /tmp/letters.txt

Create a minimal (empty) tar file

tar cf minimal.tar --files-from /dev/null

See also

cpio
Shell commands
Windows 10 also comes with tar.exe.
SQLite archives provide a similar functionalality like tar does.
jar, the Java Archive Tool.

Index