将 tar.gz 文件解压到某个目录,如果不存在则创建该目录

Dri*_*leX 2 linux bash tar

我知道我可以跑

$ tar xf somefile.tar.gz -C somedir
Run Code Online (Sandbox Code Playgroud)

将 tar.gz 文件提取到 somedir,但它需要 somedir 首先存在。

有没有办法提取到目录并在目录不存在时动态创建该目录?

小智 5

使用 GNUtar命令,您可以执行以下操作:

tar -xf somefile.tar.gz --one-top-level=somedir
Run Code Online (Sandbox Code Playgroud)

man 1 tar

  --one-top-level[=DIR]
       Extract all files into DIR, or, if used without argument, into a subdirectory named
       by  the  base name of the archive (minus standard compression suffixes recognizable
       by --auto-compress).
Run Code Online (Sandbox Code Playgroud)