LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Compress a bunch of files individually (https://www.linuxquestions.org/questions/linux-software-2/compress-a-bunch-of-files-individually-4175659699/)

MarkB2 08-23-2019 08:08 PM

Compress a bunch of files individually
 
Hi, i'm new to the forum so please forgive me if this thread is in the wrong section.

So, i have these hundred of files i need to be compressed individually into the .zip format and i couldn't find a way to do so using the zip utility on the terminal.

What i want is something like this:

File 1.png -> File 1.zip
File 2.png -> File 2.zip
File 3.png -> File 3.zip

All files have blank spaces in their names and they're all the same filetype, i think this should be taken into account. I could also compress them into the 7z format but i couldn't find how either.

Thanks in advance.

Sefyir 08-23-2019 08:56 PM

try for loops

Code:

for i in *png; do command; done
For example

Code:

for i in *png; do echo "$i".zip "$i"; done

Skaperen 08-23-2019 08:59 PM

what command are you using to compress just one file? which shell are you using? bash?

do you need to take these files in compressed form to Windows? if not, there are better ways to compress files than ZIP format.

these are PNG image files. they are already compressed. you can recompress them into JPEG format and they will use less space. but, JPEG also loses some image quality so you may not want to do that. i leave my images in the format they come in as that is usually good enough. there are ways to get them a bit smaller, but then i find it inconvenient to use them that way.

MarkB2 08-23-2019 11:27 PM

I'm using bash in linux mint. I used .png just as an example, the files i have are a bunch of .smc and .nes files and i need them to be compressed in either .zip or .7z to take less space from a SD card.

I tried Sefyir's recommendation changing the command a bit and it worked:
Code:

for i in *smc; do zip "$i".zip "$i"; done
But all the output files now have two extensions, like this:

filename.smc.zip

And i was wondering if it's also possible to remove the .smc from the output .zip filename?

permaroot 08-24-2019 01:43 AM

Deleted.

ondoho 08-24-2019 01:57 AM

^ I wouldn't do it like that.

But yeah, I would write a script.
Something like this:

Code:

#!/bin/bash

for i in *.smc *.nes; do
    zip "${i%.*}".zip "$i"
done

(not tested; read 'man zip'?)

Shadow_7 08-24-2019 02:01 PM

$ for INPUT in *.png; do OUTPUT=$(echo $INPUT | sed 's/\.png/\.zip/'); echo $INPUT $OUTPUT; done

Replace the echo command with the compression one when you're confident the parameters to the compression utility are correct.

MarkB2 08-24-2019 05:06 PM

Quote:

Originally Posted by ondoho (Post 6028873)
^ I wouldn't do it like that.

But yeah, I would write a script.
Something like this:

Code:

#!/bin/bash

for i in *.smc *.nes; do
    zip "${i%.*}".zip "$i"
done

(not tested; read 'man zip'?)

This didn't work. The terminal shows a ">" and does nothing.

Quote:

Originally Posted by Shadow_7 (Post 6029050)
$ for INPUT in *.png; do OUTPUT=$(echo $INPUT | sed 's/\.png/\.zip/'); echo $INPUT $OUTPUT; done

Replace the echo command with the compression one when you're confident the parameters to the compression utility are correct.

i don't understand how to change this line. I tried changing each echo for "7z a -mx=9" but it throws an error for each file.


I already managed to compress all files to .7z using aforementioned command so i just installed mmv and renamed everything removing the double extensions, thanks everyone.

ondoho 08-25-2019 01:11 AM

Quote:

Originally Posted by MarkB2 (Post 6029107)
This didn't work. The terminal shows a ">" and does nothing.

What I actually meant:
Save that text to a file, make it executable, and run it form the command line.
Not really necessary in this case, but more convenient if you use that particular set of commands a lot.

Shadow_7 08-29-2019 06:40 PM

If you were using say tar, you'd change:

echo $INPUT $OUTPUT;

to

tar -czvpf $OUTPUT $INPUT;

or I'd be more likely to keep the echo and add the other command after it, so if it took a while I'd know what was taking a while. The VAR= sets the VAR, where the $VAR returns the "value" of the VAR. Which doesn't have to be uppercase, I just do that to distinguish between variable and command.


All times are GMT -5. The time now is 08:13 AM.