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:
-cor--create: create an archive-xor--extract: extract an archive-tor--list: list the content in the archive
General arguments
Arguments that often used in addition to the previous list of arguments:
-for--file: read from the file or write to the file-vor--verbose: verbose output-Cor--cdor--directory: change the working directory
Compressed archives
If you want to create a compressed archive:
-aor--auto-compress: determine the compress algorithm from the archive file extension-zor--gzip: use thegzipcompression (.tar.gzarchive)-jor--bzip2: use thebzip2compression (.tar.bz2archive)-Jor--xz: use thexzcompression (.tar.xzarchive)
Examples
List an archive content
$ tar -tf mybackup.tar
mylog.log
myimage.png
myconfig.conf
mydirectory/
mydirectory/mytextfile1.txt
mydirectory/mytextfile2.txtCreate 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.txtExtract 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 filesCreate 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.txtExtract 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
Member discussion