CentOS /RHEL: How To Extract Specific Files From A Tarball(tar.gz)

I would like to extract specific files from a large tar.gz tarball file. How Do I extract files from a tarball file under CentOS 7/6.5 or RHEL 7/6.5 linux system? How to extract a single file from a tar.gz file on your linux system?

​You need to use tar tool to extract a single or specific files from a tar.gz file. Below you will see that how to extract a single file named “test1” from a tarball file named “test.tar.gz”.

CentOS /RHEL Extract specific File Using Tar Command


You firstly need to check the tarball file content which file you want to extract or if that file is included in archive file. type the following command:

tar ztf test.tar.gz

tar ztf test.tar.gz | grep test1

outputs:

[root@devops temp]# tar ztf test.tar.gz

dir1/

dir2/

dir3/

test1

test2

test3

test4

[root@devops temp]# tar ztf test.tar.gz | grep test1

test1

To extract A single file from a archive file, issue the following command:

tar -zxvf <tar filename> <file you want to extract>

tar -zxvf test.tar.gz test1

or

tar --extract --file=test.tar.gz test1

outputs:

[root@devops temp]# ls

test.tar.gz

[root@devops temp]# tar -zxvf test.tar.gz  test1

test1

[root@devops temp]# ls

test1  test.tar.gz

To extract specific files from a archive file, you can also use wildcard to extract files that matching specific pattern. For example extract all files that matching “test*” in the test.tar.gz archive file. You just need pass the “–wildcards” option to tar command, issue the following command:

tar -zxvf test.tar.gz --wildcards 'test*'

Outputs:

[root@devops temp]# ls

test1  test.tar.gz

[root@devops temp]# tar -zxvf test.tar.gz --wildcards 'test*'

test1

test2

test3

test4

[root@devops temp]# ls

test1  test2  test3  test4  test.tar.gz

See also: tar manual page  

You might also like:

Sidebar



back to top