- Create an Archive file
- Extract an Archive file
- List the contents present in an archive
- Add a file or directory in an existing archive
- Delete a file or directory from an archive
- Concatenate multiple archive files
- Compress archive files using compression tools like gzip and bzip2
- Verify an archive file
First of all, let’s focus on some of the most important options that we can use with the tar
command.
Options | Explanation |
---|---|
-c, –create | Create a new archive. |
-x, –extract, –get | Extract Files/Directories from an archive. |
-t, –list | List the contents of an archive. |
-A, –catenate, –concatenate | Append archive to the end of another archive. |
-C –directory=DIR | Extract contents(Files/Directories) to a specific directory |
–delete | Delete from the archive. |
-r, –append | Append files to the end of an archive. |
-z, –gzip | Filter the archive through gzip. |
-j, –bzip2 | Filter the archive through bzip2. |
–wildcards | Use Wildcards with tar Command |
-v, –verbose | Verbosely list files processed. |
-?, –help | Help/Manual page access |
–version | Display the version of tar Command. |
You must follow the syntax given below to use the tar
command.
tar [OPTION...] [FILE]...
To create a simple tar archive type the following command.
Here in this example, we are archiving the directory named data/ which contains these files: file1.txt, file2.txt, file3.txt, file4.txt, file5.txt.
~]# tree data data ├── file1.txt ├── file2.txt ├── file3.txt ├── file4.txt └── file5.txtand the output file that will be created after archiving is named data.tar
~$ tar -cvf data.tar data/ data/ data/file2.txt data/file3.txt data/file5.txt data/file1.txt data/file4.txtThe extension of the
tar
archive file is .tar
~$ ls data data.tar
You can use the following command to extract an existing tar archive file.
In this example, we are extracting the archive named data.tar.
~$ tar -xvf data.tar data/ data/file2.txt data/file3.txt data/file5.txt data/file1.txt data/file4.txt
To display or list the contents(files/directories) inside an archive, you can use the following command.
~$ tar -tvf data.tar drwxr-xr-x ubuntu/ubuntu 0 2020-07-10 03:11 data/ -rw-r--r-- ubuntu/ubuntu 0 2020-07-10 03:11 data/file2.txt -rw-r--r-- ubuntu/ubuntu 0 2020-07-10 03:11 data/file3.txt -rw-r--r-- ubuntu/ubuntu 0 2020-07-10 03:11 data/file5.txt -rw-r--r-- ubuntu/ubuntu 0 2020-07-10 03:11 data/file1.txt -rw-r--r-- ubuntu/ubuntu 0 2020-07-10 03:11 data/file4.txt
As mentioned at the beginning of this article, we can compress the tar
archive with the help of gzip
and bzip2
tools.
The extension of the gzip
compressed tar
archive file is .tar.gz or tgz.
Type the following command to create a compressed tar
archive from gzip
. The -z
option is used to compress the tar archive with gzip
.
~/data$ tar -czvf data.tar.gz data/Or you can also use the following command.
~/data$ tar -czvf data.tgz data/Output:
data/ data/file2.txt data/file3.txt data/file5.txt data/file1.txt data/file4.txt
The extension of the bzip2
compressed tar
archive file is .tar.bz2 or .tbz or tar.tb2 or tar.tbz.
Type any of the following commands to create a compressed tar
archive from bzip2
. The -j
option is used to compress the tar archive with bzip2
.
~$ tar -cjvf data.tar.bz2 data/OR
~$ tar -cjvf data.tar.tb2 data/OR
~$ tar -cjvf data.tar.tbz data/OR
~$ tar -cjvf data.tbz data/Output:
data/ data/file2.txt data/file3.txt data/file5.txt data/file1.txt data/file4.txt
Whether the Tar archive is gzip
compressed or bzip2
compressed we have to use the same options with the tar
command to extract/uncompress and that is -xvf
.
Let’s take some examples :
Extract tar.gz archive File:
Type the following command to extract/uncompress tar.gz
archive file.
~$ tar -xvf data.tar.gz data/ data/file3.txt data/file4.txt data/file1.txt data/file5.txt data/file2.txtExtract tar.bz2 archive File:
Type the following command to extract/uncompress tar.bz2
archive file.
~$ tar -xvf data.tar.bz2 data/ data/file3.txt data/file4.txt data/file1.txt data/file5.txt data/file2.txt
As you know, By default tar
extracts the contents in the current working directory.
But if you want to extract your contents in the specified directory, it is possible. Let’s take some examples.
Extract standard tar archive to a specified directory:
Following command will extract the data.tar(Standard Tar archive) file to mydata/files/ directory.
~$ tar -xvf data.tar -C mydata/files/ data/ data/file3.txt data/file4.txt data/file1.txt data/file5.txt data/file2.txtExtract tar.gz archive to a specified directory:
Following command will extract the data.tar.gz(gzip compressed Tar archive) file to mydata/files/ directory.
~$ tar -xvf data.tar.gz -C mydata/files/ data/ data/file3.txt data/file4.txt data/file1.txt data/file5.txt data/file2.txtExtract tar.bz2 archive to a specified directory:
Following command will extract the data.tar.bz2(bzip2 compressed Tar archive) file to mydata/files/ directory.
~$ tar -xvf data.tar.bz2 -C mydata/files/ data/ data/file3.txt data/file4.txt data/file1.txt data/file5.txt data/file2.txt
The contents of the compressed tar archive can be listed easily. Let’s take an example of each.
Type the following command to list the contents of a tar.gz
archive. In this example, we are listing the contents of data.tar.gz
~$ tar -tvf data.tar.gz drwxr-xr-x helpdesk/helpdesk 0 2021-01-26 06:25 data/ -rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file3.txt -rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file4.txt -rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file1.txt -rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file5.txt -rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file2.txtType the following command to list contents of a
tar.bz2
archive. In this example, we are listing the contents of data.tar.bz2
~$ tar -tvf data.tar.bz2 drwxr-xr-x helpdesk/helpdesk 0 2021-01-26 06:25 data/ -rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file3.txt -rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file4.txt -rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file1.txt -rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file5.txt -rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file2.txt
You can always add files/directories to an existing tar
archive. to do so use the following command.
In this example, we are adding a file named myfile.txt to data.tar
~$ tar -rvf data.tar myfile.txtAdding a directory named mydata/ to data.tar
~$ tar -rvf data.tar mydata/Note: You cannot add/update contents to an existing compressed tar archive(tar.gz/tar.bz2).
I got this error when I added a file to the compressed tar
archive
~$ tar -rvf data.tar.gz myfile.txt tar: Cannot update compressed archives tar: Error is not recoverable: exiting now
You can also remove/delete files/directories from an existing tar
archive. to do so run the following commands.
Deleting a file named myfile.txt from data.tar
~$ tar --delete -f data.tar myfile.txtDeleting a directory named mydata/ from data.tar
~$ tar --delete -f data.tar mydata/Note: You cannot remove contents from an existing compressed tar archive(tar.gz/tar.bz2).
You can use the following commands to extract a specific file/directory from a Standard or Compressed tar archive. Let’s take an example of each.
Extract file or directory from a Standard tar archive
Following command will extract two files named file2.txt and file3.txt from data.tar
~$ tar -xvf data.tar data/file2.txt data/file3.txtNote: Always remember that when extracting a specific file or directory from any
tar
archive, mention its complete path.
If you notice in the above example, we have mapped the entire path of the file to extract specific files. i.e. Both file2.txt and file3.txt are inside the data/ directory, so I had to mention data/file2.txt, data/file3.txt in the command.
To check the path of files/directories use the following command.
~$ tar -tvf data.tar drwxr-xr-x helpdesk/helpdesk 0 2021-01-27 01:58 data/ -rw-r--r-- helpdesk/helpdesk 0 2021-01-27 01:58 data/file3.txt -rw-r--r-- helpdesk/helpdesk 0 2021-01-27 01:58 data/file4.txt -rw-r--r-- helpdesk/helpdesk 0 2021-01-27 01:58 data/file1.txt -rw-r--r-- helpdesk/helpdesk 0 2021-01-27 01:58 data/file5.txt -rw-r--r-- helpdesk/helpdesk 0 2021-01-27 01:58 data/file2.txtHere in this example, we are extracting a directory named mydata/ from data.tar
~$ tar -xvf data.tar mydata/You also have to use the same command to extract specific content from a compressed archive. for example:
Extract file or directory from a tar.gz archive
~$ tar -xvf data.tar.gz data/file2.txt data/file3.txtExtract file or directory from a tar.bz2 archive
~$ tar -xvf data.tar.bz2 data/file2.txt data/file3.txt
Wildcard is a symbol or a special character using which we can match the pattern of a word or a string to get our desired output.
Some popular Wildcard Characters are Asterisk (*
), Percent (%
), Brackets ([]
)…etc.
Now let’s see how well we can use wildcard with tar
command.
Example #1
Include those files in a tar
archive whose extension is “.txt“
~$ tar -cvf myfiles.tar --wildcards *.txtExample #2
Extract all files from the archive starting with “fi“
~$ tar -xvf myfiles.tar --wildcards fi*
You can check the tar command version using the following command.
~$ tar --version
Use the following commands to access the Manual Page/Help Page of tar command.
~$ man tar
~$ tar --help