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 :
[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”typescript” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
tar -czvf tar-folder-name.tar.gz source-folder-name
[/dm_code_snippet]
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 :
[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”typescript” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
tar -xzvf tar-folder-name.tar.gz
[/dm_code_snippet]
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
[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”typescript” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
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
[/dm_code_snippet]
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
[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”typescript” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
tar -czvf tar-folder-name.tar.gz /var/www/html/Temp --exclude=/var/www/html/Temp/Temp1 --exclude=/var/www/html/Temp/Myfile.php
[/dm_code_snippet]
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 :
[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”typescript” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
tar -czvf tar-folder-name.tar.gz /var/www/html/Temp --exclude=*.php
[/dm_code_snippet]
It will compress the content of the Temp folder exclude all .php files from the Temp folder.
5) Create the tar.bz2 file :
[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”typescript” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
tar cvfj tar-bz-folder.tar.bz2 /var/www/html/php
[/dm_code_snippet]
It will compress bz2 compression with the highly compressed tar file.
5) Extract the tar.bz2 file :
[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”typescript” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
tar -xvf tar-bz-folder.tar.bz2
[/dm_code_snippet]
It will extract tar-bz-folder.bz2 tar file.
6) Extract a single file tar.gz file :
[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”typescript” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
tar -zxvf tar-folder-name.tar.gz test.php OR tar --extract --file=tar-folder-name.tar.gz test.php
[/dm_code_snippet]
It will extract test.php single file from tar-folder-name.tar.gz
7) Extract a single file tar.bz2 file :
[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”typescript” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
tar -jxvf tar-folder-name.tar.bz2 Test.php OR tar --extract --file=tar-folder-name.tar.bz2 Test.php
[/dm_code_snippet]
8) Extract a group of specific extension file from tar.gz file :
[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”typescript” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
tar -zxvf tar-folder-name.tar.gz --wildcards '*.html'
[/dm_code_snippet]
It will extract all .html file from tar-folder-name.tar.gz
[dm_code_snippet background=”no” background-mobile=”no” slim=”no” line-numbers=”no” bg-color=”#fff” theme=”dark” language=”typescript” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
tar -jxvf tar-folder-name.tar.bz2 --wildcards '*.html'
[/dm_code_snippet]
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 !!