Page tree
Skip to end of metadata
Go to start of metadata

Tar unlike zip allow you to keep permissions. As such it is the defacto utility for making backups. Also if you can keep your user UIDs consistent across environments, tar allows for quick disaster recovery.

This article still needs to be completed.

Must Read

Before using tar there are one critical behaviour to understand, always tar using relative paths. Otherwise you risk overwriting your data when untaring.

Put an example of that here with more details.

Create linux tar gz (Gzip) archive

tar -czvf myarchive.tar.gz ./mydirectory/

The f flag must come at the end or you will get an error.

Here is what each flag does,

-c, –create create a new archive


Note that .tar.gz is the same thing as .tgz.


Extract linux tar gz (Gzip) archive and note the The f flag must come at the end or you will get an error.

tar -xzvf mystuff.tgz

In some scenarios, you are moving between systems and want to extract maintaining permissions of users external to yourself. In this scenario you must ensure you UIDs are the same across systems,

sudo tar -xzvf mystuff.tgz


We use -x to extract the files form the tar archive
-x, –extract, –get extract files from an archive

Extract linux tar archive to speciefied directory
tar -xvzf filename.tar.gz -C /desired/path

And now let us shortly explain this command

Usage: tar [OPTION]… [FILE]…

Let us check the option used in this example

-c, –create                      create a new archive
-z, –gzip, –ungzip        filter the archive through gzip
-v, –verbose                   verbosely list files processed
-f, –file=ARCHIVE          use archive file or device ARCHIVE

-C directory file
Performs a chdir  operation on directory and performs the c (create) or r (replace) operation on file .
In c and r mode, this changes the directory before adding the following files. In x mode, change directories after opening the archive but before extracting entries from the archive.

Testing / viewing your archive

tar -tvf myarchive.tar
tar -tzvf myarchive.tgz

Here we used the – t opton
-t, –list                           list the contents of an archive

Backing Data

Tar can be used to backup an entire directory keeping all permissions and users accounts intact. The trick is to use sudo to keep the permissions intact.

Also, using this method, tar up a real directory, not a symbolic link to one.

In this case, order of the switches (zcvpf) does matter. Otherwise, you will get an error (record the error here) once the tar command finishes.

# To tar the directory
sudo tar -zcvpf tarfile.tar.gz ./folder/
# To untar and gunzip the file in one command
sudo tar -zxvpf tarfile.tar.gz
# Encrypting a tar

Explanation about why - v does not work in a script without output to file. -Roderick

Using tar in a backup script run in cron

sudo tar -zcpf $BACKUPDIR/nameofbackupfile.tar.gz /path/to/directory && echo "Completed OK" > /home/user/log.txt
  • No labels

1 Comment

  1. Roderick, went over your instructions. You should read up on using -p to tar the directory. It will cause issues.