0

The tar command has option to extra a file (file_name.tgz) into given directory (directory_name). ie.,

tar -zxf file_name.tgz -C directory_name

But it fails, if I try to run the above command, without creating directory (directory_name) prior to running this. Following is the error I got:

tar: directory_name: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now

Is there any option present in tar command to create this directory if not present?

The present work around I have, is by running mkdir -p directory_name in prior.

Mithun B
  • 151
  • 6
  • 1
    [How to extract files to another directory using 'tar' command](https://askubuntu.com/a/1018374/986805) –  Oct 28 '20 at 07:44
  • 2
    you can also use `mkdir -p directory && tar -xf filename.tgz -C $_`, $_ will contain the last argument of the previous command. –  Oct 28 '20 at 07:49
  • @bac0n what about the case when `mkdir` fails? It would be very rare, but still. – Mithun B Oct 28 '20 at 07:59
  • 2
    command && <-- and command, meaning if the first command fails the second command will not execute. –  Oct 28 '20 at 08:30
  • MithunB, `mkdir -p` will create the parent path too, so it will not fail, while the user has enough write permissions. @bac0n, please convert your comment into an answer. – pa4080 Oct 28 '20 at 08:30
  • @bac0n you are right about `&&`. I just overlook without paying much heed. – Mithun B Oct 28 '20 at 10:34

0 Answers0