这不起作用:
tar xf /tmp/foo.tar.gz foo/bar
tar: foo/bar: Not found in archive
Run Code Online (Sandbox Code Playgroud)
除了将其提取到位并移动文件之外,我还不清楚还有什么办法可以做到这一点。
sr_*_*sr_ 33
来自man 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.
Run Code Online (Sandbox Code Playgroud)
即,tar xC /foo/bar -f /tmp/foo.tar.gz应该做这项工作。(在 FreeBSD 上,但 GNU tar 在这方面基本相同,请参阅其手册中的“更改工作目录”)
ktf*_*ktf 10
如果您想在别处提取 tar 存档,只需cd到目标目录并在那里解压:
mkdir -p foo/bar
cd foo/bar
tar xzvf /tmp/foo.tar.gz
Run Code Online (Sandbox Code Playgroud)
您使用的命令将搜索foo/bar存档中的文件并将其解压缩。
正在做:
(cd foo/bar ; tar xf /tmp/foo.tar.gz )
Run Code Online (Sandbox Code Playgroud)
会做的工作。
基本上,什么是产生一个新的shell(括号),在这个子shell中,将目录更改为foo/bar然后解压缩文件。
您可以;通过 a更改&&以确保cd工作正常。