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

link|improve this question

feedback

2 Answers

up vote 23 down vote accepted

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
link|improve this answer
5  
+1 - There is no reason to create a tar (Tape ARchive) for a single file. Just compress what you want to compress - the result will be marginally smaller, and make infinitely more sense to people extracting the files. – voretaq7 Feb 5 at 20:28
feedback

To build on @SvenW's answer (which will only work on the current directory), if you have a HUGE number of files or want to do it on a recursive directory structure you can also use

find . -type f -exec gzip \{\} \;

and if you need to put the output into a different directory (in this example, ../target) and don't want to remove the originals, you can do something like:

find . -type f -print | while read fname ; do
    mkdir -p "../target/`dirname \"$fname\"`"
    gzip -c "$fname" > "../target/$fname.gz"
done
link|improve this answer
find . -type f -exec gzip {} \; will be sufficient. The second code will fail with filenames containing spaces and the like. – user unknown Feb 6 at 2:29
Some shells don't like bare {}s, and the second one does something markedly different than the first (and I was aware of the spacing issues but that is kind of tricky to deal with). – fluffy Feb 6 at 3:07
If you know such a shell, I invite you to answer my question over here: unix.stackexchange.com/questions/8647/… . Nobody was able to name something which is today in use so far. If you are aware of a risk of your answer, it might be a good idea to name it in your answer. – user unknown Feb 6 at 4:20
@userunknown I was brought up on (t)csh, which had quite a lot of trouble with unescaped curly braces at one time, although it doesn't appear to anymore (but I still escape {} as a force of habit). Others have already posted those as answers to your question, however. – fluffy Feb 6 at 6:55
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.