The tar command in Linux is used to create an archive of files and directories. With the “tar” command you can create an archive file in the following formats: “.tar“,“.tar.gz“,“.tar.bz2”. They are known as “tarball“,“gizp“,“bzip”In Linux. Tar is more flexible than ZIP, which is widely used in a cross-platform environment.
We will show tar command examples in this article.
First, see how many options are available to the command
Main operating mode:
- -A, -catenate, -concatenate Add files to tar archive
- -c, -create create a new archive
- -d, -diff, -compare find differences between archive and filesystem
- -delete delete from archive (not on tapes!)
- -r, -append add files to the end of the archive
- -t, -list list archive contents
- -test-label check archive and exit volume label
- -u, -update only add newer files to archive
- -x, –extract, –get extract files from archive
- -a, -auto-compress suffix use archive to define program compression
- -I, –use-compress-program = PROG filter over PROG (should accept -d)
- -j, -bzip2 filter for archiving via bzip2
- -J, -xz filter archives through XZ
- -lzip filter archives via Lzip
- -lzma filter archives through lzma
- -no-auto-compress do not use archive suffix to define compression program
- -z, -gzip, -gunzip, -ungzip filter archive via GZIP
- -Z, -compress, -uncompress filter archive through compression
I am choosing the WordPress installer for the demo. The total size of the installer is 26MB. We will check the zipped file size of each suggested compression method.
1. Create a file archive with tar
Archive Tar does compression of files and folders automatically. Archived can be stored on disk or can be transferred to another disk or it can be used for email attachments.
tar -caf wordpress.tar wordpress/
The archive size is reduced to 23MB.
[[email protected] andreyex]# ls -lh -rw-r--r-- 1 root root 23M Jan 5 22:12 wordpress.tar
2. Create a tar.gz file archive
GZ stands for Gunzip. .tar.gz archive can compress files and folders in an efficient way.
tar -czf wordpress.tar.gz wordpress/
The archive size is reduced to 7.6Mb. Pretty impressive!
[[email protected] andreyex]# ls -lh -rw-r--r-- 1 root root 23M Jan 5 22:09 wordpress.tar -rw-r--r-- 1 root root 7.6M Jan 5 22:15 wordpress.tar.gz
3. Create tar.bz2 archive
BZIP is more efficient to reduce the size of the archive file.
tar -cjf wordpress.tar.bz2 wordpress/
The archive size is reduced to 6.9MB.
[[email protected] andreyex]# ls -lh -rw-r--r-- 1 root root 23M Jan 5 22:09 wordpress.tar -rw-r--r-- 1 root root 6.9M Jan 5 22:27 wordpress.tar.bz2 -rw-r--r-- 1 root root 7.6M Jan 5 22:26 wordpress.tar.gz
4. Untar tar file archive
Convenience and ease of use of any archive, if useful, when it can be easily unpacked. Unzip the file .tar
tar -xf wordpress.tar or tar -xf wordpress.tar /home/andreyex/extracted
The ‘X’ stands for unpacking.
5. Unpack the tar.gz archive file
tar -xzf wordpress.tar.gz
6. Unpacking tar.bz2 file archive
tar -xjf wordpress.tar.bz2
7. Contents of the archive file list
tar tf wordpress.tar wordpress/ wordpress/wp-blog-header.php wordpress/wp-login.php wordpress/wp-config-sample.php wordpress/wp-admin/ wordpress/wp-admin/link-parse-opml.php wordpress/wp-admin/ms-upgrade-network.php wordpress/wp-admin/themes.php
8.Untar a specific file from a tar archive file
tar -xf wordpress.tar wordpress/wp-config-sample.php
That’s all for now. These are the main tar command examples on Unix / Linux.