2 min read

Tar general arguments

Tar general arguments

Can't remember how to create or extract an archive? This short article can help you to refresh your memory about some of the essential tar arguments.

Required arguments

One of these arguments are required to run the tar command:

  • -c or --create: create an archive
  • -x or --extract: extract an archive
  • -t or --list: list the content in the archive

General arguments

Arguments that often used in addition to the previous list of arguments:

  • -f or --file: read from the file or write to the file
  • -v or --verbose: verbose output
  • -C or --cd or --directory: change the working directory

Compressed archives

If you want to create a compressed archive:

  • -a or --auto-compress: determine the compress algorithm from the archive file extension
  • -z or --gzip: use the gzip compression (.tar.gz archive)
  • -j or --bzip2: use the bzip2 compression (.tar.bz2 archive)
  • -J or --xz: use the xz compression (.tar.xz archive)

Examples

List an archive content

$ tar -tf mybackup.tar
mylog.log
myimage.png
myconfig.conf
mydirectory/
mydirectory/mytextfile1.txt
mydirectory/mytextfile2.txt

Create an archive

$ tree
.
├── myconfig.conf
├── mydirectory
│   ├── mytextfile1.txt
│   └── mytextfile2.txt
├── myimage.png
└── mylog.log

2 directories, 5 files

$ tar -cf mybackup.tar mylog.log myimage.png myconfig.conf mydirectory

$ tar -tf mybackup.tar
mylog.log
myimage.png
myconfig.conf
mydirectory/
mydirectory/mytextfile1.txt
mydirectory/mytextfile2.txt

Extract an archive

$ tar -tf mybackup.tar
mylog.log
myimage.png
myconfig.conf
mydirectory/
mydirectory/mytextfile1.txt
mydirectory/mytextfile2.txt

$ mkdir unpacked

$ tar -xf mybackup.tar -C unpacked

$ tree unpacked
unpacked
├── myconfig.conf
├── mydirectory
│   ├── mytextfile1.txt
│   └── mytextfile2.txt
├── myimage.png
└── mylog.log

2 directories, 5 files

Create a compressed archive

$ tree
.
├── myconfig.conf
├── mydirectory
│   ├── mytextfile1.txt
│   └── mytextfile2.txt
├── myimage.png
└── mylog.log

2 directories, 5 files

$ tar -caf mybackup.tar.gz mylog.log myimage.png myconfig.conf mydirectory

$ tar -tf mybackup.tar.gz
mylog.log
myimage.png
myconfig.conf
mydirectory/
mydirectory/mytextfile1.txt
mydirectory/mytextfile2.txt

Extract a compressed archive

$ tar -tf mybackup.tar.gz
mylog.log
myimage.png
myconfig.conf
mydirectory/
mydirectory/mytextfile1.txt
mydirectory/mytextfile2.txt

$ mkdir unpacked

$ tar -xf mybackup.tar.gz -C unpacked

$ tree unpacked
unpacked
├── myconfig.conf
├── mydirectory
│   ├── mytextfile1.txt
│   └── mytextfile2.txt
├── myimage.png
└── mylog.log

2 directories, 5 files