NetAdminTools.com
 
SignalQ Sites:
L1G3R Information Systems - Coprolite - SpotBridge - NAW
RoboCoop - AreWeDown - SolarPower - SysAdminTools
Xfig - Gold Loaf - GeekPapa - FixGMC - FixRambler
Solar Energy - Energy Efficiency - Solar Panels
Home Energy Savings
Categories:
GNU/Linux | Homebrew designs | Perl | Ruby | Administration | Backup/Recovery | Bugs/Fixes | Certification | Database | Email | File/Print | Hardware | Information Grab Bag | Interoperability | GNU/Linux ABCs | Monitoring | Name Resolution | Network Services | Networking | Remote Control | Security | Desktop | Web | BSD | Solaris | GIAGD | ERP | REALbasic

Last 30 Days | Last 60 Days | Last 90 Days | All Articles | RSS


Categories:
·GNU/Linux
·Homebrew designs
·Perl
·Ruby
·Administration
·Backup/Recovery
·Bugs/Fixes
·Certification
·Database
·Email
·File/Print
·Hardware
·Information Grab Bag
·Interoperability
·GNU/Linux ABCs
·Monitoring
·Name Resolution
·Network Services
·Networking
·Remote Control
·Security
·Desktop
·Web
·BSD
·Solaris
·GIAGD
·ERP
·REALbasic
·All Categories


Using Tar to Extract a Single File or Directory
Topic:GNU/Linux   Date: 2009-11-08
Printer Friendly: Print   

spacerspacer
<<  <   >  >>

Subject

It may just be me... but... One thing I tend to do is extract entire tarballs, find the file I want, and then copy the file. Tar can be used to extract a single file from the archive. Further, it will extract the tree. Let's take a peek at the archive using head and the t option:

srv-8:STORE2 usr4$ tar -tjf non*.bz2 | head
sources/
sources/zlib-1.2.3.tar.gz
sources/glibc-2.3.5.tar.bz2
sources/shadow-4.0.18.1-useradd_fix-2.patch
sources/CleanSourceTrees.sh
sources/findutils-4.4.1.tar.gz
sources/less-429.tar.gz
.
.
.

We know the structure. Likely everything is under sources. We want sources/mcj.rsd. Let's make sure we can find it first using t and v options:

srv-8:STORE2 usr4$ tar -tjvf non*.bz2 sources/mcj.rsd
-rwxr-xr-x root/root  52097024 2009-11-03 20:45:16 sources/mcj.rsd
srv-8:STORE2 usr4$ 

Note that there is no sources directory:

srv-8:STORE2 usr4$ ls
ct				mcjnonic.rsd
glcomp.txt			mcjo.rsd
glibc.html			network
glibc_files			ngg
journal				nonic
mcj				nonic3.1.sources.tar.bz2
mcjl				tkit
mcjngg.rsd

The extraction will create the directory:

srv-8:STORE2 usr4$ tar -xjf nonic*.bz2 sources/mcj.rsd
srv-8:STORE2 usr4$ ls
ct				mcjnonic.rsd
glcomp.txt			mcjo.rsd
glibc.html			network
glibc_files			ngg
journal				nonic
mcj				nonic3.1.sources.tar.bz2
mcjl				sources
mcjngg.rsd			tkit
srv-8:STORE2 usr4$ ls sources
mcj.rsd
srv-8:STORE2 usr4$ 

Note that you can match and extract multiple files:

srv-8:STORE2 usr4$ tar -tjvf non*.bz2 sources/XFree*
-rw-r--r-- root/root  11985968 2009-05-22 19:50:47 sources/XFree86-4.8.0-src-4.tgz
-rw-r--r-- root/root   3163472 2009-05-22 19:50:47 sources/XFree86-4.8.0-src-6.tgz
-rw-r--r-- root/root   5907777 2009-05-22 19:50:44 sources/XFree86-4.8.0-src-2.tgz
-rw-r--r-- root/root  10618247 2009-05-22 19:50:43 sources/XFree86-4.8.0-src-1.tgz
-rw-r--r-- root/root  12840281 2009-05-22 19:50:46 sources/XFree86-4.8.0-src-3.tgz
-rw-r--r-- root/root   4680606 2009-05-22 19:50:48 sources/XFree86-4.8.0-src-7.tgz
-rw-r--r-- root/root   4351386 2009-05-22 19:50:47 sources/XFree86-4.8.0-src-5.tgz
srv-8:STORE2 usr4$

To extract from a directory on down, use:

tar -xjf archive.tar.bz2 dir/subdir/


People:
Places:
Things:
Times:





Please read our Terms of Use and our Privacy Policy
Microsoft, Windows, Windows XP, Windows 2003, Windows 2000, and NT are either trademarks or registered trademarks of Microsoft Corporation. NetAdminTools.com is not affiliated with Microsoft Corporation. Linux is a registered trademark of Linus Torvalds, and refers to the Linux kernel. The operating system of most distributions that contain the Linux kernel is GNU/Linux. All logos and trademarks in this site are property of their respective owner. Copyright 1997-2011 NetAdminTools.com