tljubas tljubas - 4 months ago 15
Bash Question

In place untar and delete tar (or tar.gz)

I have this tiny code here

for i in *.tar.gz;
do tar xzvf $i;
done && find . -name "*.tar.gz" -exec rm {} \;


Now, when I have multiple tars, it will first untar all of them and then delete the tar files.

How can I change the code to untar a file, to delete it and then move to the next tar file?

Thanks in advance

Answer
for file in *.tar.gz; do tar xzvf "${file}" && rm "${file}"; done

Don't forget to quote your variables to account for funky filenames with whitespace.