Linux Basics: 14 Tar Command Examples in Linux
I don’t think any Linux admin requires an introduction of Tar command. Anyway, in simple words, Tar (Tape Archive) is a widely used command line tool to create backup archives (collection of files and directories).
By default, a tar archive file contains uncompressed files. Together with gzip and bzip2, you can create a compressed archive.
14 Tar Command Examples
In this guide, we will see the essential tar commands examples which include how to create tar files with (tar, tar.gz, and tar.bz2) compression.
Let us start with simple to advanced tar command examples.
1. Create Tar Archive file
Below command will create a noncompressed .tar archive file called raj_home.tar from files present /home/raj/ directory.
tar -cvf raj_home.tar /home/raj/
Output:
/home/raj/ /home/raj/.bash_logout /home/raj/.bash_profile /home/raj/.bashrc /home/raj/.bash_history /home/raj/itzgeek_demo.sql /home/raj/important.sh /home/raj/myfile /home/raj/epel-release-latest-6.noarch.rpm
c => Create a new archive file
f => File name of the archive file
v => Show progress of command
2. Create tar.gz Archive file
We can create a compressed archive .tar.gz/.tgz in two ways. Below command will create a compressed .tar archive file called raj_home.tar.gz from files present /home/raj/ directory.
New tar.gz archive file: Both .tar.gz and .tgz are same.
tar -zcvf raj_home.tar.gz /home/raj/
OR
tar -zcvf raj_home.tgz /home/raj/
z => Compress the .tar file with gzip
From an existing tar file:
gzip raj_home.tar
3. Create tar.bz2 Archive file
The tar command with bzip2 creates a highly compressed archive file. The size of tar.bz2 file will be far less than .tar.gz. We can create a compressed archive tar.bz2/.tbz/.tb2 in two ways.
Below command will create compressed .tar archive file called raj_home.tar.bz2 from files present /home/raj/ directory.
New .tar.bz2 archive file:
tar -jcvf raj_home.tar.bz2 /home/raj/
OR
tar -jcvf raj_home.tbz /home/raj/
OR
tar -jcvf raj_home.tb2 /home/raj/
j => Compress the .tar file with bzip2
From an existing .tar file:
bzip2 raj_home.tar
4. View tar Archive file
Below command will list the content of .tar archive file raj_home.tar without extracting.
tar -tvf raj_home.tar
5. View tar.gz Archive file
Below command will list the content of tar.gz/.tgz archive file without extracting.
# viw tar.gz file tar -ztvf raj_home.tar.gz # view .tgz file tar -ztvf raj_home.tgz
6. View tar.bz2 Archive file
Below command will list the content of .tar.bz2/.tbz/.tb2 archive file without extracting.
# view tar.bz2 file tar -jtvf raj_home.tar.bz2 # view tbz file tar -jtvf raj_home.tbz # view tb2 file tar -jtvf raj_home.tb2
7. Extract tar Archive file
Below command will extract .tar archive file raj_home.tar in the current directory.
tar -xvf raj_home.tar
8. Extract tar.gz Archive file
Below command will extract .tar.gz/.tgz in the working directory.
tar -zxvf raj_home.tar.gz
9. Extract tar.bz2 Archive file
Below command will extract .tar.bz2/.tbz/.tb2 file in the current working directory.
tar -jxvf raj_home.tar.bz2
10. Extract particular file or folder from tar/tar.gz/tar.bz2 archive file
Extracting bigger archive files takes a lot of disk space and time-consuming task. You can save both disk space and time by extracting a particular file or folder from the tar file.
Before extracting the file, view the content of the archive file and decide which file you want to extract.
Below command extracts myfile from raj_home.tar{.gz,.bz2}.
Extract particular file or folder from tar:
tar -xvf raj_home.tar home/raj/myfile
Extract particular file or folder from tar.gz:
tar -zxvf raj_home.tar.gz home/raj/myfile
Extract particular file or folder from tar.bz2:
tar -jxvf raj_home.tar.bz2 home/raj/myfile
The extracted file or folder can be found in the current working directory with the same directory structure. ie. <CWD>/home/raj/myfile.
11. Extract multiple files or folders from tar/tar.gz/tar.bz2 archive file
Below command extracts myfile and rajfolder from raj_home.tar{.gz,.bz2}.
Extract multiple files or folders from tar:
tar -xvf raj_home.tar "home/raj/myfile" "home/raj/rajfolder"
Extract multiple files or folders from tar.gz:
tar -zxvf raj_home.tar.gz "home/raj/myfile" "home/raj/rajfolder"
Extract multiple files or folders from tar.bz2:
tar -jxvf raj_home.tar.bz2 "home/raj/myfile" "home/raj/rajfolder"
Extracted files and folders can be found in the current working directory with the same directory structure. ie. <CWD>/home/raj/.
12. Extract tar/tar.gz/tar.bz2 file to another directory
By default, extracted files and folders are found in the current working directory. You can use below command to extract tar file to your desired directory.
Extract tar file to another directory:
tar -xvf raj_home.tar -C /tmp
Extract tar.gz file to another directory:
tar -zxvf raj_home.tar.gz -C /tmp
Extract tar.bz2 file to another directory:
tar -jxvf raj_home.tar.bz2 -C /tmp
13. Add file or folder to the existing tar archive file
Below command will add a file or folder to the end of an existing tar file.
tar -rvf raj_home.tar /var/log/message
14. Exclude certain files/folders tar archive file
You can use –exclude flag to exclude files and folders from archiving.
Excluding specific files/folders in a tar file:
tar --exclude="/home/raj/rajfolder" -cvf raj_new_home.tar /home/raj
Excluding specific files/folders in a tar.gz file:
tar --exclude="/home/raj/rajfolder" -zcvf raj_new_home.tar.gz /home/raj
Excluding specific files/folders in a tar.bz2 file:
tar --exclude="/home/raj/rajfolder" -jcvf raj_new_home.tar.bz2 /home/raj
That’s All. You can also use man tar command or see the online manual to get more information about tar.