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'm currently using this command

    tar -zxvf foo.tgz -C /root/dir1/ dir2/dir3/dir4/dir5/file

to extract my specific file from a tar archive to a given directory, dir1. As is, I end up with root/dir1/dir2/dir3/dir4/dir5/file but what I am after is root/dir1/file. This is important because I have 1000 of these files and I don't want to have to open all the individual directories to get to them. Does anyone know how to extract just the file?

Many thanks.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

With GNU tar, you can use --strip-components to remove a specific number of directory levels before the filename. Otherwise, use -O and redirect the output into the desired file.

share|improve this answer
    
Thanks! --strip-components works in regular tar too –  user1792403 Dec 13 '13 at 19:02
    
@user1792403: No, it doesn't. –  Ignacio Vazquez-Abrams Dec 13 '13 at 19:06
    
oh, then i must be in GNU tar without realising? i haven't loaded gnu though –  user1792403 Dec 13 '13 at 20:14
    
@user1792403: If you're running Linux, you're using GNU tools. –  Ignacio Vazquez-Abrams Dec 13 '13 at 20:52
    
Oh sorry, thank you for all this information –  user1792403 Dec 13 '13 at 21:36

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.