I have a tar.gz-file with the following structure:
folder1/img.gif
folder2/img2.gif
folder3/img3.gif
I want to extract the image files without the folder hierarchy so the extracted result looks like:
/img.gif
/img2.gif
/img3.gif
I need to do this with a combination of Unix and PHP. Here is what I have so far, it works to extract them to the specified directory but keeps the folder hierarchy:
exec('gtar --keep-newer-files -xzf images.tgz -C /home/user/public_html/images/',$ret);
Check the tar version e.g.
If version is >= than tar-1.14.90 use
--strip-components
else use
--strip-path
Based on
@ford
's answer. This one will extract it to the my_dirname folder. So that we can properly clear the empty folders without affected currently existing files.You can use the --strip-components option of tar.
I create a tar file with a similar structure to yours:
Then extracted by doing:
This is almost possible with tar alone, using the --transform flag, except that there's no way to delete the left over directories as far as I can tell.
This will flatten the entire archive:
The output will be
You will then need to delete the directories with another command, unfortunately.
Find img*.gif in any sub folder of mytar.tar.gz and extract to ./
Find img*.gif in any of the 3 folders listed in this specific question in mytar.tar.gz and extract to ./