How to zip each of the files?

Hello,

I have over 200 files under one directory. And I need to zip all of them individually, like image1.jpg > image1.zip, image2.jpg > image2.zip etc.

Is there a quick command I can use under Linux shell that would let me do this faster?

ls -1d image*.jpg 2>/dev/null | while read FILENAME
do
         if [ -f "${FILENAME}" ]
         then
                # Place zip command here with $FILENAME as a variable
         fi
done

Go the necessary directory and fire the below command...

ls -1 | awk ' { print "zip "$1".zip " $1 } '  | sh
for i in *; do gzip $i; done
ls | xargs -I file gzip file;

methyl: It return nothing. Nothing happens. No errors.
jayan_jay:

sh: line 1: zip: command not found
sh: line 2: zip: command not found
sh: line 2: .zip: command not found
sh: line 3: zip: command not found
sh: line 4: zip: command not found
sh: line 5: zip: command not found
sh: line 6: zip: command not found
sh: line 7: zip: command not found
...
...
...
sh: line 101: zip: command not found

itkamaraj: It tries to gzip all the files with each of the filenames, for example;

gzip: Yeniden: No such file or directory
gzip: Insan: No such file or directory
gzip: Insana: No such file or directory
gzip: compressed data not written to a terminal. Use -f to force compression.

Where the file is called Yeniden Insan Insana

Sorry for being a pain :confused:

Try this,

find . -name "*.jpg" -exec gzip {} \;

Also, replace the dot (.) to directory which you want.

1 Like

Rksiva: That worked! Thank you so much!

Another way..

ls -1 /path/to/image/files/*.jpg | xargs -n1 gzip

One more question; how do I do this into .zip, not .gz? The program I'm tryin to import these files into does not accept gzip.

it seems your machine dont have the zip utility.
sh: line 1: zip: command not found
sh: line 2: zip: command not found
(You pasted this before)

Aju,

Just try this command to zip the file instead of gzip

find . -name "*.jpg" -exec sh -c 'echo "zip $0.zip $0" | sed 's,\.\/,,g'' {} \; | sh

Rksiva: That command creates a single rar file with a file inside it that consists of "zip <filename>" filename is all the names of the files that I have.

jayan_jay: I've just installed zip from my distro's repos. And the command returns as:

 xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

zip error: Nothing to do!

how about this:

go to the particular directory and

 
ls *.jpg | xargs -I zip {};

panyam: Yet again,

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

It sounds like your file names have " ' " in it!!..

Did you try with -0 option then?

ls *.jpg | xargs -0 zip {};
for i in *.jpg; do zip $i; done

Aju,

This command wont create a single rar file,

For e.g.

I'm having 2 jpg files, and it would create the zip file as below

-rw-rw-rw- 1 user staff 0 Jun 28 16:10 a.jpg
-rw-rw-rw- 1 user staff 0 Jun 28 16:10 b.jpg

-rw-rw-rw- 1 user staff 142 Jun 28 16:10 a.jpg.zip
-rw-rw-rw- 1 user staff 142 Jun 28 16:10 b.jpg.zip

panyam: Returns the same error.

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option zip error: Nothing to do!

rksiva: zip warning: name not matched

Rksiva: What is does for me is to create a single file. With a extentionless file inside it.

Edit: I'm guessing it may be related to m file name / extension.

So the files I'm trying to zip are not a single word. They got spaces etc. And they are epub file, not JPG

ok,

try with "find option" for files with spaces.

 
find . -name "*.jpg" -exec zip {} \;