Strip first directory from tar

I was trying to find this the other day – how to extract a tar and strip the first directory off. For example, WordPress extracts to the wordpress/ base directory by default and that can be annoying because you then have to move the content to the right place and remove the wordpress directory.

But there is an answer and it comes in the form of an option to pass to tar:
--strip-components [number]

So instead of something like:
tar -xvf wordpress-3.0.4.tar.gz
cp -r wordpress/* .
rm -Rf wordpress

You can just do:
tar -xvf wordpress-3.0.4.tar.gz --strip-components 1

Thought that might be useful for others (it certainly is for me!)..

3 thoughts on “Strip first directory from tar

  1. That awesome moment when you find exactly what you were looking for in less than a minute. Thank you 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *