0

I have a double-compressed file (.tar.gz inside a .tar) and i would like to know if there is any way to automatically extract it from a bash script. I tried two approaches:

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` 

But i get the error: tar: /file_inside.tar.gz: Cannot open: No such file or directory.

Thanks in advance.

1 Answer 1

2

If you know relative filename of intermediate file, try

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

where

   -O, --to-stdout
          Extract files to standard output.
  1. you might need -z flag in second tar

  2. please note that - is synonym for stdin, maybe that what you meant (tar -xf $1 | tar -xf xargs (*) ) ?

    tar -xf $1 | tar -xf - 
    

(*) xargs is backquoted


If you feel adventurous, you can also try option --to-command=COMMAND

tar -xf $1 --to-command="tar -xzf -" path/to/file
  1. I advise to test before going "production"
  2. Do not use absolute path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.