An archive file is nothing but a collection of files and directory stored in one file. Please note that by default the archive file is not compressed. A compressed file uses less disk space.
This quick page explains how to compress, list and extract files using the tar command on Linux or Unix-like system.
Compress files with tar at the shell prompt
For archiving files at the shell prompt, use the tar command as follows. The syntax is:
tar -cvf fileName.tar file1 file2 file3
tar -cvf fileName.tar dir1 dir2 dir3
tar -cvf fileName.tar file1 dir1
Where,
- c : Create a new archive
- v : Verbose output
- f : Use given archive file or device such as /dev/st0
Linux compress files with tar
For example, the c option requires creating the archive, the v option requests the verbose operation, and the f option takes an argument that sets the name of the archive to operate upon. The following command, written in the traditional style, instructs tar to store all files from the directory /home/vivek/Documents into the archive file docs.tar verbosely listing the files being archived:
$ tar -cvf docs.tar /home/vivek/Documents/
Sample outputs:
tar: Removing leading `/' from member names
/home/vivek/Documents/
/home/vivek/Documents/info-needed/
/home/vivek/Documents/info-needed/1.pdf
/home/vivek/Documents/info-needed/1508600013963.pdf
/home/vivek/Documents/info-needed/2.pdf
/home/vivek/Documents/info-needed/SLF02A056010087822.pdf
/home/vivek/Documents/info-needed/server.pdf
/home/vivek/Documents/info-needed/chair-bill.pdf
/home/vivek/Documents/info-needed/ebay-apc.ups.pdf
/home/vivek/Documents/info-needed/SLF02A056020116881.pdf
/home/vivek/Documents/info-needed/t2016-17.pdf
/home/vivek/Documents/info-needed/1508600031526.pdf
/home/vivek/Documents/info-needed/Shipment invoice.pdf
/home/vivek/Documents/info-needed.zip
/home/vivek/Documents/cc-details
/home/vivek/Documents/2016-17.zip
/home/vivek/Documents/Scanned Document.pdf
The following adds file named /etc/resolv.conf, /etc/hosts, and /home/vivek/project1 directory:
$ tar -cvf backup.tar /etc/resolv.conf /etc/hosts /home/vivek/project1
Compressing Files at the Shell Prompt
For compressing files you need to use the compression utilities:
- bzip2 – tar.bz2 file
- gzip – tar.gz file
- zip – tar.zip file
To compress the tar file with gzip pass the -z option, run:
$ tar -cvzf docs.tar.gz /home/vivek/Documents/
To compress the tar file with bzip2 pass the -j option, run:
$ tar -cvjf docs.tar.bz2 /home/vivek/Documents/
To list the contents of a tar file, enter:
$ tar -tvf docs.tar
$ tar -tvzf docs.tar.gz
$ tar -tvjf docs.tar.bz2
To extract the contents of a tar file, enter:
$ tar -xvf docs.tar
$ tar -xvzf docs.tar.gz
$ tar -xvjf docs.tar.bz2
Where,
- t : List the contents of an archive
- x : Extract a tar archive
- z : Compress the tar file with gzip
- j : compress the tar file with bzip2
Conclusion
This page explained the basic syntax of tar, and I hope you learned how to compress files with tar command running on Linux, FreeBSD, OpenBSD, macOS, and Unix-like operating systems. The tar command has many more options. Hence, I suggest that you read the tar command help page here.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 1 comment... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Are you still stuck with tar command? Do you need further help? Try our forum.