Advertisement
pebriana

Extract Without First Directory

Oct 7th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. [ url ] : http://www.marksanborn.net/linux/extract-without-first-directory/
  2.  
  3.  
  4.  
  5.  
  6. Extract Without First Directory
  7.  
  8. Jan 6th, 2009
  9.  
  10. Whenever I download something that is compressed on the Internet in a .zip, .rar or .tar.gz it is always a crapshot whether or not it contains a “container directory”. A “container directory” is a directory that contains all the other files usually with the same name as the compressed file.
  11.  
  12. For example the Zend Framework when downloaded contains a folder called, ’ZendFramework-1.7.2’. All the other files are contained under this folder. This is great but sometimes I want to extract the contents of the folder without the “container folder”.
  13.  
  14. This is how I used to extract the contents and remove the “container folder”:
  15.  
  16. tar -xvf ZendFramework-1.7.2.tar.gz
  17.  
  18. Get rid of the tarball…
  19.  
  20. rm ZendFramework-1.7.2.tar.gz
  21.  
  22. cd ZendFramework-1.7.2/
  23.  
  24. Which would result in:
  25.  
  26. Zend Framework Directory Structure
  27.  
  28. Copy everything in the “container” folder and move it up a directory.
  29.  
  30. cp -rf * ../
  31.  
  32. Now I have found a better way…
  33. A better way
  34.  
  35. The flag that I have learned is the strip flag. This will strip off the first directory and extract the rest.
  36.  
  37. tar -xvf ZendFramework-1.7.2.tar.gz --strip 1
  38.  
  39. The only thing now is… How do I tell if a tar contains a “container folder”?
  40.  
  41. Easy
  42.  
  43. tar -tf ZendFramework-1.7.2.tar.gz | head
  44.  
  45. This will list contents of the file ’ZendFramework-1.7.2.tar.gz’ showing only the first few lines.
  46.  
  47. What do you think? Is there an even better way?
  48.  
  49. Posted by Mark Sanborn Jan 6th, 2009 linux
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement