tar (tar Ubuntu Linux command tutorials)


  • A ".tar" file is not a compressed files, it is a collection of files within a single file uncompressed.

  • However if the file is a .tar.gz ("tarball") or ".tgz" file it is a collection of files that is compressed.

  • This command will help us to create backup of files to same or other directory or computer.

  • If you are looking to compress a file you would create the tar file with the z option or create the file and then gzip the file.


Creating a tar file

tar -cvf file.tar myfile.txt

This command would unarchive (untar) the myfile.tar file into the current directory.

The following snapshot shows the working of tar command


Backup all files from home /sheetal directory

For example

tar zcvpf homebackup.tar.gz /home/sheetal



z Compress the backup file with 'gzip' to make it smaller.

c Create a new backup archive called /backup/files.backup.Nov_6_2009.tar.gz.

v Verbose mode, the tar command will display what it's doing to the screen.

p Preserves the permissions of the files put in the archive for restoration later.

f homebackup.tar.gz: Specifies where and filename to store the backup.




To see list of files stored in Tar ball or an Archive

For example

tar -ztvf homebackup.tar.gz


How to create compressed tar.gz file from multiple files and folders in Ubuntu Linux?

In order to create compressed tar.gz files from multiple files and folders, apply command as follows

For example

tar -czf newbackup_filename.tar.gz file1 file2 file3 folder1 folder2 folder3


How to exclude certain files or directories in Ubuntu Linux

If you want to exclude some folder or specific types for files, for example you want to exclude /music folder while taking backup and also wants to exclude all *.mp4 files.

tar - - exclude='/home/sheetal/music/' - - exclude='*.mp4' zcvpf homebackup.tar.gz /home/sheetal



Extracting or Restore the files from a tar file in Ubuntu Linux

tar -xzf backup_file_name.tar.gz