Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

shell

unix

sh

gzip

tar

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

like image 545
tljubas Avatar asked Mar 07 '14 13:03

tljubas


People also ask

How do I delete a tar GZ file?

Simply right-click the item you want to compress, mouseover compress, and choose tar. gz. You can also right-click a tar. gz file, mouseover extract, and select an option to unpack the archive.

What is the difference between tar and tar GZ?

In short, a TAR file creates one archive file out of multiple files without compressing them, while the GZ file format compresses a file without creating an archive. Combined into the tar GZ file extension, you can archive and compress multiple files into one.

Can you delete tar files?

We can also remove files from within the tar file with the --delete option. In the below example we remove the test2. txt file then list the contents of archive. tar which now only has test1.

What is tar and untar?

Untar is defined as a command which enables users to extract files that are compressed with tar, tar. gz, tar. bz2 formats of compression. This command is used for 2 specific utilities in file operations.


2 Answers

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.

like image 120
Adrian Frühwirth Avatar answered Sep 22 '22 20:09

Adrian Frühwirth


Simply change the order of actions:

for i in *.tar.gz; do
  tar xzvf "$i" && rm -r "$i"
done
like image 23
bagage Avatar answered Sep 23 '22 20:09

bagage