Compress Each File in Directory into Its Own Compressed File on Linux

linuxtar

How do I compress every file in a directory into its own tar whilst preserving the name for each file?

i.e.
file1.out
file2.out

–>

file1.out.tar.gz
file2.out.tar.gz

Best Answer

Putting every file into a separate tar file doesn't make any sense in this scenario. You can use gzip to compress them directly:

gzip *

will result in file1.out.gz, file2.out.gz etc.

You would use tar only if you would need a compressed archive as a single file.

If you ineed need a tar archive for every file, you can create it like so:

for i in *; do tar -czf $i.tar.gz $i; done