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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.