tags:

views:

118

answers:

3

Patches are frequently released for my CMS system. I want to be able to extract the tar file containing the patched files for the latest version directly over the full version on my development system. When I extract a tar file it puts it into a folder with the name of the tar file. That leaves me to manually copy each file over to the main directory. Is there a way to force the tar to extract the files into the current directory and overwrite any files that have the same filenames? Any directories that already exist should not be overwritten, but merged...

Is this possible? If so, what is the command?

+2  A: 

Check out the --strip-components (or --strippath) argument to tar, might be what you're looking for.

EDIT: you might want to throw --keep-newer into the mix, so any locally modified files aren't overwritten. And I would suggest testing new releases on a development server, then using rsync or subversion to carry over the changes.

snemarch
Incredible - I've been looking for this functionality for awhile - this feature is undocumented in the man file on my systems, but it works. Thank You!
jmohr
A: 

The option '--strip-components' allows you to trim parts of the embedded filenames. With that it is possible to do what you want.

For more help check out http://www.gnu.org/software/tar/manual/html_section/transform.html

Bryan Oakley
Cool. Will the directories be merged and files overwritten in a patch-like manner?
Nothing is merged per se, but whatever is in the tar file will overwrite what is currently on disk (assuming you have the proper permissions). The best thing to do is make a copy of your site and experiment a little.
Bryan Oakley
A: 

I tried getting --strip-components to work and, while I didn't try that hard, I didn't get it working. It kept flattening the directory structure. In searching, I came across the following command that seems to do exactly what I want:

pax -r -f patch.tar -s'/patch\///'

It's not tar, but hey, it works... Replace the words "patch" with whatever your tar file name is.

Did you remember to specify a level for --strip-components? If the archive is unpacked to foo/<files>, you'd use "--strip-components 1".
snemarch
Yes, I did try --strip-components 1 and the results were not what I expected. If I remember right, it flattened the directory structure by one layer and messed up the file structure in the process. As I said, though, I didn't dig in that far before I found the other approach that worked for me.