TAR is short for Tape Archive and is a good tool for archiving and compressing files for backup use. Also, software that is downloaded from the internet can come compressed and you can use the tar command on the terminal to unpack these compressed files. In this tutorial, we will go over some basic TAR commands that will come in handy.
First, let's open up a terminal by clicking on the bottom left. Then up top in the search bar, type terminal, and click on the terminal icon to open.
Archiving files and folders using TAR
Basic Syntax for TAR is shown below. To archive more than one file you just need to add a space
tar [options] [archive file name] [files to archive]
In the example below, we will be using sudo to bypass permission issues.
sudo tar cvf my_backup.tar file1 file2 file3
- c – short for create (create archive)
- v – short for verbose (show verbose output)
- f – short for filename (name of the archived file)
To verify that the files were archived, we will use the t option to list folders in the .tar file we created.
sudo tar -tf my_backup.tar
We can also back up directories. In this example, I will back up my /Documents folder
sudo tar cvf backup_docs.tar home/user/Documents
Compressing files and folders using tar along with gzip
The tar file is not a compressed file. We will need to use gzip to compress our .tar backup using the z option
sudo tar cvzf my_docs.tgz home/user/Documents
Above you can see the my_doc.tgz file that was created and the verbose output that has been compressed successfully.
Unpacking a compressed file using tar
To extract or unpack a file you can use the x option. In these examples, we will extract a file contained in the .tar archive. The x options are also used to unpack gzip files.
sudo tar xvf my_backup.tar file1
To unpack a .tar or .tgz file and move it to a new directory we will use the -C option. First, let's make a directory
sudo mkdir /home/user/backupShells
Next, let's unpack and move our my_docs_new.tgz file to that directory.
sudo tar xvf my_docs_new.tgz -C home/sguzman/backupShells
And that’s how you create archives, compressed files, and unpack them.