In this tutorial, Today I will explain how to create and extract tar files in Linux. In Linux/Ubuntu there are so many times, we used to create tar.gz, tar.bz2 file, etc.
Let’s check in details about tar command :
You may also like this :
1) Create the tar.gz file :
tar -czvf tar-folder-name.tar.gz source-folder-name
It will compress the contents of source-folder-name to a tar.gz archive named tar-folder-name.tar.gz
Here,
2) Extract the tar.gz file :
tar -xzvf tar-folder-name.tar.gz
It will extract the archive to the folder tar-folder-name.tar.gz
3) Create the tar.gz with multiple directories or files at once
tar -czvf tar-folder-name.tar.gz /var/www/html/source-folder-name-1 /var/www/html/source-folder-name-2 /var/www/html/source-folder-name-3/temp.php
It will compress the content of multiple folders to the tar-folder-name.tar.gz
4) Create the tar.gz with Exclude Directories and Files
tar -czvf tar-folder-name.tar.gz /var/www/html/Temp --exclude=/var/www/html/Temp/Temp1 --exclude=/var/www/html/Temp/Myfile.php
It will compress the content of Temp folder exclude Temp1 Folder and Myfile.php file.
Now, If you want to exclude all files with specific file extensions. Then, you can use this below command :
tar -czvf tar-folder-name.tar.gz /var/www/html/Temp --exclude=*.php
It will compress the content of the Temp folder exclude all .php files from the Temp folder.
5) Create the tar.bz2 file :
tar cvfj tar-bz-folder.tar.bz2 /var/www/html/php
It will compress bz2 compression with the highly compressed tar file.
5) Extract the tar.bz2 file :
tar -xvf tar-bz-folder.tar.bz2
It will extract tar-bz-folder.bz2 tar file.
6) Extract a single file tar.gz file :
tar -zxvf tar-folder-name.tar.gz test.php
OR
tar --extract --file=tar-folder-name.tar.gz test.php
It will extract test.php single file from tar-folder-name.tar.gz
7) Extract a single file tar.bz2 file :
tar -jxvf tar-folder-name.tar.bz2 Test.php
OR
tar --extract --file=tar-folder-name.tar.bz2 Test.php
8) Extract a group of specific extension file from tar.gz file :
tar -zxvf tar-folder-name.tar.gz --wildcards '*.html'
It will extract all .html file from tar-folder-name.tar.gz
tar -jxvf tar-folder-name.tar.bz2 --wildcards '*.html'
It will extract all .html file from tar-folder-name.tar.bz2
Here, I explain short quick details about tar options
- c: create an archive file.
- x: extract an archive file.
- v: show the progress of the archive file.
- f: filename of the archive file.
- j: filter archive through bzip2.
- z: filter archive through gzip.
- wildcards: Specify patterns in the tar command.
That’s it !!
I hope this blog is easy to understand about how to create and extract tar files in Linux. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.
Keep liking and sharing !!