LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-23-2019, 08:08 PM   #1
MarkB2
LQ Newbie
 
Registered: Aug 2019
Distribution: Linux Mint
Posts: 5

Rep: Reputation: Disabled
Unhappy 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.
 
Old 08-23-2019, 08:56 PM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
try for loops

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

Code:
for i in *png; do echo "$i".zip "$i"; done
 
1 members found this post helpful.
Old 08-23-2019, 08:59 PM   #3
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
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.
 
1 members found this post helpful.
Old 08-23-2019, 11:27 PM   #4
MarkB2
LQ Newbie
 
Registered: Aug 2019
Distribution: Linux Mint
Posts: 5

Original Poster
Rep: Reputation: Disabled
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?

Last edited by MarkB2; 08-23-2019 at 11:48 PM.
 
Old 08-24-2019, 01:43 AM   #5
permaroot
Member
 
Registered: Aug 2019
Location: Arden, NC
Distribution: Arch Linux
Posts: 129

Rep: Reputation: 48
Deleted.

Last edited by permaroot; 08-24-2019 at 01:52 AM.
 
Old 08-24-2019, 01:57 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
^ 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'?)
 
1 members found this post helpful.
Old 08-24-2019, 02:01 PM   #7
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
$ 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.
 
1 members found this post helpful.
Old 08-24-2019, 05:06 PM   #8
MarkB2
LQ Newbie
 
Registered: Aug 2019
Distribution: Linux Mint
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by ondoho View Post
^ 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 View Post
$ 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.

Last edited by MarkB2; 08-24-2019 at 05:14 PM.
 
Old 08-25-2019, 01:11 AM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by MarkB2 View Post
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.
 
Old 08-29-2019, 06:40 PM   #10
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me - zip all files individually in directories and subfolders femina.jaffer@gmail.com Linux - Newbie 4 09-01-2018 07:55 PM
Restarting Apache Virtual Hosts Individually libsys Linux - Software 1 04-12-2005 04:46 AM
installing software individually on FC3 ninadb Fedora 4 02-07-2005 04:45 PM
Compiling Kernel Modules Individually taillefer Programming 1 04-12-2004 06:18 AM
MRTG for multiple IPs graphed individually newb13 *BSD 3 07-28-2003 05:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration