PDA

View Full Version : Extract Single Files from Tar Archive


carouzal
08-29-2005, 07:38 PM
Can anyone give me a hand getting a single file out of a tar archive?
I have found several resources but can not get it working.

acme.mail.order
08-29-2005, 08:39 PM
It's in the manual, but not very intuitive.

tar -x ./filename/to/extract -f tarfile.tar

If you don't supply a filename to the -x argument it extracts everything. Otherwise it extracts the specified file, which must exactly match the tar directory listing.

It will also recreate the folder structure to the single file unless you use the -O (stdout) option and direct that to a different file.

carouzal
08-29-2005, 09:20 PM
Command:
tar -x ./Users/jmdevaney/Sites/index.html -z -v -f backup.tgz

Returns:
tar: ./Users/jmdevaney/Sites/index.html: Not found in archive
tar: Error exit delayed from previous errors

Any ideas?

/usr/bin/tar xfvz backup.tgz '*.html'

the previous command will extract all .html files but if I use 'index.html' or anything with a full path it fails.

This is just a test archive small and simple to get the hang of this.

hayne
08-29-2005, 09:26 PM
tar -x ./Users/jmdevaney/Sites/index.html -z -v -f backup.tgz

Returns:
tar: ./Users/jmdevaney/Sites/index.html: Not found in archive

When using 'tar' it is always a good idea to use 'tar -t' to display a list of the contents of the archive. This will show the exact path names of the files inside.
I'm guessing that the path name of the file you are interested in is
"/Users/jmdevaney/Sites/index.html"
not
"./Users/jmdevaney/Sites/index.html"

acme.mail.order
08-30-2005, 04:08 AM
or if you are lazy (and sure that there are no duplicated filenames)

tar -x *index.html -zf backup.tgz

Wether or not there are full paths in the archive depends on how the archive was created. Think carefully about wether you want full paths in an archive or not - I would think for website backups you do not.

Gheto Fabulous
08-23-2009, 03:10 AM
This is one hundred years too late but will help me when I next forget :)
Just leave off the leading "/" and it will work.
eg. Users/Blah/Blarhdy/fileyouwant