命令
tar xf tarfile.tar /home/parent_dir/child_dir
Run Code Online (Sandbox Code Playgroud)
只会提取文件child_dir及其下属.
如果/home/parent_dir/child_dir不是你想要的那样,GNU tar提供了一个--transform可以使用的选项:
tar --transform 's,/home/parent_dir/child_dir,foo,' --show-transformed -xf tarfile.tar
Run Code Online (Sandbox Code Playgroud)
这将把已经进入的文件/home/parent_dir/child_dir放入其中./foo.
cd <another_directory>
tar xvf <path_to_tar>/<tarfile>.tar <child_dir>
例如
cd <parent_directory>
tar cvf test.tar *
tar tf test.tar
查看您想要的文件夹。例如,src/org
cd <some other directory you want to extract to>
tar xvf ..\test.tar src/org
ls
您现在将看到 tar 中您所在的目录,例如src/org