How do I extract files without folder structure using tar

LinuxUnixTar

Linux Problem Overview


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);

Linux Solutions


Solution 1 - Linux

You can use the --strip-components option of tar.

> --strip-components count > (x mode only) Remove the specified number of leading path ele- > ments. Pathnames with fewer elements will be silently skipped. > Note that the pathname is edited after checking inclusion/exclu- > sion patterns but before security checks.

I create a tar file with a similar structure to yours:

$tar -tf tarfolder.tar
tarfolder/
tarfolder/file.a
tarfolder/file.b

$ls -la file.*
ls: file.*: No such file or directory

Then extracted by doing:

$tar -xf tarfolder.tar --strip-components 1
$ls -la file.*
-rw-r--r--  1 ericgorr  wheel  0 Jan 12 12:33 file.a
-rw-r--r--  1 ericgorr  wheel  0 Jan 12 12:33 file.b

Solution 2 - Linux

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:

tar xzf images.tgz --transform='s/.*\///'

The output will be

folder1/
folder2/
folder3/
img.gif
img2.gif
img3.gif

You will then need to delete the directories with another command, unfortunately.

Solution 3 - Linux

Check the tar version e.g.

$ tar --version

If version is >= than tar-1.14.90 use --strip-components

tar xvzf web.dirs.tar.gz -C /srv/www --strip-components 2

else use --strip-path

tar xvzf web.dirs.tar.gz -C /srv/www --strip-path 2

Solution 4 - Linux

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.

tar xzf images.tgz --transform='s/.*\///' -C my_dirname
find my_dirname -type d -empty -delete

Solution 5 - Linux

Find img*.gif in any sub folder of mytar.tar.gz and extract to ./

> tar -zxf mytar.tar.gz --absolute-names --no-anchored img*.gif > --transform='s:.*/::'

Find img*.gif in any of the 3 folders listed in this specific question in mytar.tar.gz and extract to ./

> tar -zxf mytar.tar.gz --absolute-names --no-anchored img*.gif > --transform='s:^folder[1-3]/::'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBen JacksonView Question on Stackoverflow
Solution 1 - LinuxericgView Answer on Stackoverflow
Solution 2 - LinuxfordView Answer on Stackoverflow
Solution 3 - LinuxMinimulView Answer on Stackoverflow
Solution 4 - LinuxWeston GangerView Answer on Stackoverflow
Solution 5 - LinuxPanchoView Answer on Stackoverflow