Linux

簡單的 tar 提取失敗,找不到文件

  • December 13, 2020

我有一個名為 playground.tar 的壓縮包,文件如下:

測試.txt

當我提取所有內容時沒有問題,但是當我嘗試提取單個文件時,當我執行此命令時會引發以下錯誤:

tar xf playground.tar test.txt

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

tar:由於先前的錯誤而以失敗狀態退出

什麼給了我也嘗試過用引號括起來的路徑。該文件肯定存在。

編輯

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

tar cf 遊樂場.tar 遊樂場

您需要指定整個路徑:

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!

引用自:https://unix.stackexchange.com/questions/624253