How to list and extract a single file from a tar ball in linux ?

Many of the times we have a very large tar ball backup and we want just a small file/directory from it to accomplish our task. For example:- you have taken complete backup of your filesystem in a tar file which is around ten GB and you came to know that your pam.d directory gets corrupted and wants to restore it back. So the good practice is to simply extract pam.d directory from the tar file, instead of extracting complete archive. 
 
	So here I will show you how to extract a single file/directory from a tar file. 
 
	1.       Lets suppose we have a tar file of firefox. 
 
	# ls -l firefox-18.0.2.tar 
 
	-rw-r--r-- 1 ejaisak egi 43991040 Feb  7 15:48 firefox-18.0.2.tar 
 
	  
 
	2.       Now we should know which file/directory we want to extract. For this, we can run below command. Suppose we want to extract a directory components. So first check if it exists In the tar file. 
 
	# tar --list --file=firefox-18.0.2.tar | grep -i components 
 
	firefox/components/ 
 
	firefox/components/libdbusservice.so 
 
	firefox/components/libmozgnome.so 
 
	firefox/components/binary.manifest 
 
	firefox/components/libbrowsercomps.so 
 
	firefox/webapprt/components/ 
 
	  
 
	3.       Now we will extract this directory from the tar file with the below command. 
 
	# tar --extract --file=firefox-18.0.2.tar  firefox/components/  
 
	Or, 
 
	# tar -xvf firefox-18.0.2.tar firefox/components/ 
 
	# ls -l firefox/components/ 
 
	total 132 
 
	-rw-r--r-- 1 root root   103 Feb  1 22:02 binary.manifest 
 
	-rwxr-xr-x 1 root root 57232 Feb  1 22:02 libbrowsercomps.so 
 
	-rwxr-xr-x 1 root root 25372 Feb  1 22:02 libdbusservice.so 
 
	-rwxr-xr-x 1 root root 44320 Feb  1 22:02 libmozgnome.so 
 
	# 
 
	Here we can see that only the required directory is extracted. We can change the path where we want to extract this by mentioning the path with “-C”argument. Instead of directory, we can also mention the name of any file we want to extract. 
 
	 Now you can also extract single file or directory from any tar or gzip file. For gzip archive, we need to add “-z”option with tar command. 
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments