Tar Command Basics
tar command (tape archive) is used to convert a group of files into an archive.
Syntax: tar [options] [tar-archive-name] [other-file-names]
The following command creates a single archive backup file called
my_home_directory.tar under /tmp. This archive will contain all the files and
subdirectories under /home/mehrdust
- Option c, stands for create an archive.
- Option v stands for verbose mode, displays additional information while executing the command.
- Option f indicates the archive file name mentioned in the command.
# tar cvf /tmp/my_home_directory.tar /home/mehrust
How do I view all the files inside the tar archive?
Option t will display all the files from the tar archive.
# tar tvf /tmp/my_home_directory.tar
How do I extract all the files from a tar archive?
Option x will extract the files from the tar archive as shown below. This will extract the content to the current directory location from where the command is executed.
# tar xvf /tmp/my_home_directory.tar
How do I extract tar.gz files to a specific directory?
# tar xvfz /tmp/my_home_directory.tar.gz –C /home/ramesh
Combine gzip, bzip2 with tar
How to use gzip with tar?
Add option z to the tar command when dealing with tar.gz compressed file.
# tar cvfz /tmp/my_home_directory.tar.gz /home/mehrdust # tar xvfz /tmp/my_home_directory.tar.gz # tar tvfz /tmp/my_home_directory.tar.gz
Note: Using gzip is faster when compared to bzip2.
How to use bzip2 with tar?
Add option j to the tar command when dealing with tar.bz2 compressed file.
# tar cvfj /tmp/my_home_directory.tar.bz2 /home/mehrdust # tar xvfj /tmp/my_home_directory.tar.bz2 # tar tvfj /tmp/my_home_directory.tar.bz2
Note: Using bizp2 gives higher level of compression when compared to gzip.