Linux 如何使用tar提取没有文件夹结构的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14295771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I extract files without folder structure using tar
提问by Ben Hymanson
I have a tar.gz-file with the following structure:
我有一个具有以下结构的 tar.gz 文件:
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:
我需要结合 Unix 和 PHP 来做到这一点。这是我到目前为止所拥有的,它可以将它们提取到指定的目录,但保留文件夹层次结构:
exec('gtar --keep-newer-files -xzf images.tgz -C /home/user/public_html/images/',$ret);
采纳答案by ericg
You can use the --strip-componentsoption of tar.
您可以使用tar的--strip-components选项。
--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.
--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 文件:
$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
回答by ford
This is almostpossible 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.
这几乎可以单独使用 tar 使用 --transform 标志,但据我所知,没有办法删除剩余的目录。
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.
不幸的是,您将需要使用另一个命令删除目录。
回答by Minimul
Check the tar version e.g.
检查 tar 版本,例如
$ tar --version
If version is >=than tar-1.14.90 use --strip-components
如果版本>=比 tar-1.14.90 使用--strip-components
tar xvzf web.dirs.tar.gz -C /srv/www --strip-components 2
else use --strip-path
否则使用 --strip-path
tar xvzf web.dirs.tar.gz -C /srv/www --strip-path 2
回答by Weston Ganger
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.
基于@ford
的回答。这会将其解压缩到 my_dirname 文件夹中。这样我们就可以在不影响当前现有文件的情况下正确清除空文件夹。
tar xzf images.tgz --transform='s/.*\///' -C my_dirname
find my_dirname -type d -empty -delete
回答by Pancho
Find img*.gif in any sub folder of mytar.tar.gz and extract to ./
在 mytar.tar.gz 的任意子文件夹中找到 img*.gif 并解压到 ./
tar -zxf mytar.tar.gz --absolute-names --no-anchored img*.gif --transform='s:.*/::'
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 ./
在 mytar.tar.gz 中此特定问题中列出的 3 个文件夹中的任何一个中查找 img*.gif 并解压缩到 ./
tar -zxf mytar.tar.gz --absolute-names --no-anchored img*.gif --transform='s:^folder[1-3]/::'
tar -zxf mytar.tar.gz --absolute-names --no-anchored img*.gif --transform='s:^folder[1-3]/::'