LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-19-2020, 10:07 AM   #1
Dareios
LQ Newbie
 
Registered: May 2020
Posts: 1

Rep: Reputation: Disabled
Bash to group files by name and zip them


Hello,

I am looking for a way to go through a folder containing image files and ZIP them based on a substring. I started doing this using Applescript and Automator, but this was a very slow process. I came across the following Bash command

Code:
for i in *.JPG; do  zip "${i%.*}.zip" "${i%.*}".*; done
which zips each file individually. And I came across this command

Code:
for q in *_*.JPG; do mkdir -p "${q%_*}"; mv "$q" "${q%_*}"; done
The latter makes folders based on the common denominator.

As a programming newbie, what can I change in the ZIP command to achieve grouping the files and zipping them. The file names consist of an object number, following by underscore and a running number which may be up to three digits depending on the files concerning a specific object number (e.g., XYZ04999_01.jpg, XYZ04999_02.jpg, XYZ04999_03.jpg, etc.).

I'd be grateful for some help.

Thank you!
 
Old 05-19-2020, 10:23 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,845
Blog Entries: 3

Rep: Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403Reputation: 2403
A slight variant would be the standard tar archive:

Code:
for q in *_*.jpeg; do 
        tar --append -vf "${q%_*}.tar" ${q}
done
gzip *.tar
The % there is part of parameter substitution and strips away the underscore and everything else to the end of the string. If that does not build the name you want then you can try a different method or build up a variable using several methods before passing it to tar.
 
Old 05-19-2020, 12:24 PM   #3
shruggy
Member
 
Registered: Mar 2020
Posts: 994

Rep: Reputation: Disabled
FYI, zip works like tar -append by default adding new files to an existing archive if specified on the command line, so the tar line above could be rewritten as
Code:
zip "${q%_*}.zip" ${q}
 
Old 05-19-2020, 03:11 PM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 14,616
Blog Entries: 9

Rep: Reputation: 4094Reputation: 4094Reputation: 4094Reputation: 4094Reputation: 4094Reputation: 4094Reputation: 4094Reputation: 4094Reputation: 4094Reputation: 4094Reputation: 4094
A side issue: JPGs are already compressed; zipping a group of them together might be useful, but it will compress only marginally.
 
2 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How can I have zip -d file.zip "__MACOSX*" work on all zip files in directory? thomwblair Linux - Newbie 10 10-08-2018 02:30 PM
[SOLVED] remove readme files from a large group of *.zip files with a command line function thealmightyos Linux - General 1 08-14-2014 04:24 PM
search file name with same first 5 char and zip them barathan Linux - Newbie 2 12-15-2013 05:34 AM
How to zip several files to several zip with one command? thomas2004ch Linux - Newbie 4 02-08-2010 08:13 AM
Zip gives error "-bash: /usr/bin/zip: Argument list too long" konathamsrinu Programming 3 07-06-2006 10:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:00 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration