简单的 tar 提取失败,找不到文件

简单的 tar 提取失败,找不到文件

我有一个名为 Playground.tar 的 tarball,其中包含以下文件:

测试.txt

当我提取所有内容时没有问题,但是当我尝试提取单个文件时,运行此命令时会抛出以下错误:

 tar xf playground.tar test.txt

tar:test.txt:在存档中找不到

tar:由于先前的错误而以失败状态退出

我也尝试过用引号引起来的路径。该文件肯定存在。

编辑

tarball 是使用以下命令创建的:

tar cf 游乐场.tar 游乐场

答案1

您需要指定整个路径:

tar -xf playground.tar playground/test.txt

您可以使用tar --listtar -t列出存档的内容以查看其中的内容:

$ tar -tf playground.tar
playground/
playground/text.txt

以下是我为重现您的问题所做的操作的完整日志:

$ cd $(mktemp -d)                              # Go to a new empty directory
$ mkdir playground
$ touch playground/test.txt                    # Make the file we will tar

$ tar cf playground.tar playground             # Make the tar
$ tar -tf playground.tar                       # List the contents of a tar
playground/
playground/test.txt                            # There's our file! It has a full path

$ rm -r playground                             # Let's delete the source so we can test extraction
$ tar -xf playground.tar playground/test.txt   # Extract that file
$ find .                                       # Check if the file is now there
.
./playground.tar
./playground
./playground/text.txt                          # Here it is!

或者,您不需要打包整个目录。这也行得通。我还添加了test2.txt显示整个目录未解压的内容。

$ cd $(mktemp -d)                 # New directory
$ touch test.txt test2.txt        # Let's make a few files
$ tar -cf playground.tar *.txt    # Pack everything
$ tar -tf playground.tar          # What's in the archive?
test2.txt
test.txt                          # Look: No directory!
$ rm *.txt                        # Clear the source files to test unpacking
$ tar -xf playground.tar test.txt # Unpack one file (no directory name)
$ find .
.
./test.txt
./playground.tar                  # There it is!

答案2

tarball 名称必须包含在文件路径中:

tar xf 游乐场.tar操场/测试.txt

相关内容