Zip Multiple Files (linux)


b._.o._.b

Well-Known Member
Joined
Jul 6, 2006
Messages
1,157
I'd like to know the easiest way to compress multiple files without adding the individual files into one big zip file. So:

file1.xyz
file2.dfs
file3.ddd

should become

file1.zip
file2.zip
file3.zip

I tried something like zip *.*, but that didn't work.
 
b_o_b said:
I'd like to know the easiest way to compress multiple files without adding the individual files into one big zip file. So:

file1.xyz
file2.dfs
file3.ddd

should become

file1.zip
file2.zip
file3.zip

I tried something like zip *.*, but that didn't work.
man bash, look for, 'for'.%#
for i in file?.*; do g=${i#file};d=${g%.*};zip file${d}.zip $i; done
no make that;
for i in file?.*; do f=${i%file?};zip ${f}.zip $i; done
even easier.
 
Last edited by a moderator:
Thanks nice bash script, but I don't think I was clear enough in explaining what I need. Not only the extension can be different, but the naming is also not consistent.

Example.

temp.sh
picture.jpg
etc.c

should become

temp.zip
picture.zip
etc.zip

Hope it will not be too hard to adjust the script.

btw to make it even more difficult, a name could also be something like temp.bin.sh.jpg and should then become temp.bin.sh.zip
 
b_o_b said:
Thanks nice bash script, but I don't think I was clear enough in explaining what I need. Not only the extension can be different, but the naming is also not consistent.

Example.

temp.sh
picture.jpg
etc.c

should become

temp.zip
picture.zip
etc.zip

Hope it will not be too hard to adjust the script.

btw to make it even more difficult, a name could also be something like temp.bin.sh.jpg and should then become temp.bin.sh.zip
Simple enough:

for i in *.*; do f=${i%.*};echo zip ${f}.zip $i; done

is the debug version, when you find the zip command you like change to:

for i in *.*; do f=${i%.*};zip ${f}.zip $i; done

OR
create a text file name zipem.sh of:
#!/bin/bash
for i in *.*
do
f=${i%.*}
echo zip ${f}.zip $i
#zip ${f}.zip $i
done

run as:
$ bash zipem.sh
or execute a
$ chmod +x zipem.sh
to make it executable and run as
$ ./zipem.sh
remove the # comment marker when ready to do some zipping.

I recommend Barry Rosenberg's "KornShell Programming Tutorial", Addison Wesley publisher, good solid, "how do I", reference. You'll find yourself snapping off little one liners like that on the command line without even thinking about it in no time.


Squidge said:
Run Winrar or Total Commander under Wine :D
Aye, Linux is not for everybody.
 
Last edited by a moderator:
Works great!
Only problem is zip doesn't seem to like spaces in filenames and those files are not zipped, is there a solution for this as well?
 
Never had a bsod :)

So yep, pretty sure. I am using Ubuntu 7.10

This is the result of a test file I tried to zip:

the file is 'test test test.py'

zip warning: name not matched: test
zip warning: name not matched: test.zip
zip warning: name not matched: test
zip warning: name not matched: test
zip warning: name not matched: test.py

zip error: Nothing to do! (test.zip)
 
b_o_b said:
Never had a bsod :)

So yep, pretty sure. I am using Ubuntu 7.10

This is the result of a test file I tried to zip:

the file is 'test test test.py'

zip warning: name not matched: test
zip warning: name not matched: test.zip
zip warning: name not matched: test
zip warning: name not matched: test
zip warning: name not matched: test.py

zip error: Nothing to do! (test.zip)
Piece of cake, just have to quote things a bit, sometimes easier to:

ls -1 | while read i; do f="${i%.*}";zip "${f}".zip "$i"; done
 
Last edited by a moderator:
Yes! That's it. Amazing how powerful one line of code can be.
Thanks a lot, this will be a big time saver for me :)
 
I've been playing with a new utility I found, atool:

http://www.nongnu.org/atool/

It is a perl script suite that acts as a universal wrapper around various file compression formats (tar, zip, 7z, etc). The idea is you only have to remember one set of specific switches for packing/unpacking.

It has a "for each" command switch that does almost what you want (but it doesn't allow you to strip the original extension like the bash scripts from sphinxter)

atool -a -F .zip -e *

That will create:

file1.bin.zip
file2.rom.zip

from a dir containing

file1.bin
file2.rom

Almost as good! Another thing I find great about this utility is that it does "smart extraction" if someone has archived a huge tree of subfolders but weren't kind enough to put them into a containing subfolder (this happens from time to time and is very very annoying)
 
Back
Top