Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I downloaded a tarball(abc.tar.gz) which has a root folder(abc) and multiple subfolders(a, b, c, etc). And I have a folder called Target in machine(macbook pro), with multiple subfolders(a, b, c, etc) either.
Now I want to unarchive the tarball to Target folder and auto merge the subfolders. How can I achieve it? I've tried to unarchive it in Target but result with a subfolder(abc) in Target:

cp abc.tar.gz ~/Target
cd ~/Target
tar xzvf abc.tar.gz

Result:

Target/abc/a
Target/abc/b
Target/a
Target/b

What I want is:

Target/a    # auto merged
Target/b    # auto merged

Thanks in advance!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Unfortunately, tar will always unpack the full archived path even when you only request a single resource inside a subfolder path. e.g.:

$ tar -x abc/a -zvf abc.tar.gz 

will unpack only subfolder a but it's parent directory abc will still be created.

From the Target directory you can do

$ for d in `ls abc`; do cp -rf abc/$d/* ./$d/; done

to copy the archive folders into pre-existing, same-name folders in the current folder Target

or simply

$ cp -rf abc/* ./

to create all archive folders in ~/Target. If a folder named, say, 'foo' exists in both abc and Target then the contents of abc/foo will be copied to Target/foo. Be aware, however, that at the file level abc/foo/bar.txt will overwrite Target/foo/bar.txt without notice or warning.

share|improve this answer
    
mv ./abc/a ./a will not work in mac, it just move the folder ./abc/a into ./a as a subfolder. Instead I use cp -rf abc/a/* ./a/(or just cp -rf abc/* ./). but I feel it is little bit cumbersome.. Anyway, thanks for your suggestion. :D –  Kjuly Dec 26 '11 at 10:22
    
i updated my answer to reflect this as a one-line loop. BTW which shell are you using? perhaps mv ./abc/a ./ (destination is simply the current directory) should work? –  venzen Dec 26 '11 at 10:50
    
Thanks @verzen, my shell is bash. And I tried mv ./abc/a ./ with error:mv: rename ./abc/a to ./a: Directory not empty. cp -rf abc/* ./ works well. Maybe you can add this cl to your answer and I'll accept it if there's no better answer. :p –  Kjuly Dec 26 '11 at 11:03
1  
answer updated. This behavior of bash in MacOSX is interesting... I see in this post that a shopt -s dotglob nullglob might be needed for bash in OSX to behave as expected... –  venzen Dec 26 '11 at 11:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.