Question

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

Was it helpful?

Solution

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.

OTHER TIPS

Simply change the order of actions:

for i in *.tar.gz; do
  tar xzvf "$i" && rm -r "$i"
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top