How do I extract files without folder structure using tar How do I extract files without folder structure using tar linux linux

How do I extract files without folder structure using tar


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.tartarfolder/tarfolder/file.atarfolder/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


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.gifimg2.gifimg3.gif

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


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