如何使用tar提取没有文件夹结构的文件 [英] How do I extract files without folder structure using tar
本文介绍了如何使用tar提取没有文件夹结构的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有以下结构的tar.gz文件:
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
我需要结合使用Unix和PHP.到目前为止,这是我可以将其提取到指定目录但保留文件夹层次结构的方法:
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);
推荐答案
您可以使用tar的-strip-components 选项.
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.
我创建了一个与您的结构相似的tar文件:
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
这篇关于如何使用tar提取没有文件夹结构的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文