將 .tar.gz 提取到 .tar 檔案中

將 .tar.gz 提取到 .tar 檔案中

我有一個雙重壓縮檔案(.tar.gz在 a 內.tar),我想知道是否有任何方法可以自動從 bash 腳本中提取它。我嘗試了兩種方法:

tar -xf $1 #the first .tar file is passed as argument to the bash script
tar -xf /home/user/working_dir/file_inside.tar.gz #passing the absolute path to the file assuming the .tar file was extracted as expected.
tar -xf $1 | tar -xf `xargs` 

但我收到錯誤:tar: /file_inside.tar.gz: Cannot open: No such file or directory

提前致謝。

答案1

如果您知道中間檔案的相對檔案名,請嘗試

tar -xf $1 -O path/to/file.tar.gz | tar xf - 

在哪裡

   -O, --to-stdout
          Extract files to standard output.
  1. 你可能需要-z在第二個 tar 中標記

  2. 請注意,這-是 stdin 的同義詞,也許這就是您的意思 ( tar -xf $1 | tar -xf xargs(*) ) ?

    tar -xf $1 | tar -xf - 
    

(*) xargs 被反引號


如果你喜歡冒險,你也可以嘗試選項--to-command=COMMAND

tar -xf $1 --to-command="tar -xzf -" path/to/file
  1. 我建議在“生產”之前進行測試
  2. 不要使用絕對路徑

相關內容