我写了我的第一个脚本:
find /media/temp/test -type f -printf '%T@ %p\n*.tar.gz' | sort -n | tail -1 | cut -f2- -d" " | xargs tar -xvpf
我想找到最新的修改后的文件.tar.gz,然后将其解压缩到某些目录中,而不是/ media / temp / test中。
假设命令的其余部分正常工作,则只需替换最后一部分
xargs tar -xvpf
和
(cd /certain/directory; xargs tar -xvpf)
tar命令将从该目录执行。这是执行相对于另一个目录的命令的常见有用“技巧”。
请注意,这仅在这种情况下有效,因为您的tar文件名(在这种情况下,是find命令的输出)是绝对路径。如果它是相对路径名,则上面的“ cd”将不起作用。
tar
已经有一个参数可以使用:
-C directory
In c and r mode, this changes the directory before adding the following files. In x mode, change directories after opening
the archive but before extracting entries from the archive.
因此,您也可以将最后一部分
xargs tar -C /certain/directory -xvpf
检查并测试您的tar版本;平台之间存在细微的差异(您不指定使用的是哪个unix变体)。希望存档中的路径名也是相对的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句