Ben*_*son 50 php unix linux tar
我有一个tar.gz文件,其结构如下:
folder1/img.gif
folder2/img2.gif
folder3/img3.gif
Run Code Online (Sandbox Code Playgroud)
我想提取没有文件夹层次结构的图像文件,因此提取的结果如下所示:
/img.gif
/img2.gif
/img3.gif
Run Code Online (Sandbox Code Playgroud)
我需要结合Unix和PHP来做到这一点.这是我到目前为止,它的工作原理是将它们提取到指定的目录,但保留文件夹层次结构:
exec('gtar --keep-newer-files -xzf images.tgz -C /home/user/public_html/images/',$ret);
Run Code Online (Sandbox Code Playgroud)
eri*_*icg 94
您可以使用tar 的--strip-components选项.
Run Code Online (Sandbox Code Playgroud)--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文件:
$tar -tf tarfolder.tar
tarfolder/
tarfolder/file.a
tarfolder/file.b
$ls -la file.*
ls: file.*: No such file or directory
Run Code Online (Sandbox Code Playgroud)
然后通过做提取:
$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
Run Code Online (Sandbox Code Playgroud)
for*_*ord 19
使用-transform标志只能使用tar,这几乎是可能的,但据我所知,没有办法删除左侧目录.
这将使整个存档变平:
tar xzf images.tgz --transform='s/.*\///'
Run Code Online (Sandbox Code Playgroud)
输出将是
folder1/
folder2/
folder3/
img.gif
img2.gif
img3.gif
Run Code Online (Sandbox Code Playgroud)
不幸的是,您将需要使用其他命令删除目录.
Min*_*mul 12
检查tar版本,例如
$ tar --version
Run Code Online (Sandbox Code Playgroud)
如果版本> =而不是tar-1.14.90使用--strip-components
tar xvzf web.dirs.tar.gz -C /srv/www --strip-components 2
Run Code Online (Sandbox Code Playgroud)
否则使用 --strip-path
tar xvzf web.dirs.tar.gz -C /srv/www --strip-path 2
Run Code Online (Sandbox Code Playgroud)