Friday, December 2, 2016

linux - Operating on wildcard input files and saving output as + suffix


I want to batch process my a list of wildcard input files and then output then to the input filename with a suffix appended. I'm hoping to understand this general case.


In particular, I'm running



convert *.jpg -resize 800x600 ... *-resized.jpeg

Under Fedora21 with ImageMagick 6.6.8 is nice in that it appends a number suffix but I am looking to preserve the original filename and add a suffix. i.e. It outputs *-resized-0.jpeg *-resized-1.jpeg ...


Checked this does not appear applicable,
Multiple input files and output files in awk


edit: Is this possible or do I need to write a script?


Answer



It depends on how you define the word "script".  Arguably, this is a script:


for f in *.jpg
do
convert "$f" -resize 800x600 ... "${f%.*}"-resized.jpeg
done

but you can just type it like that into your terminal. 
The command (or any number of commands) between the do and the done
get(s) executed once for each file that matches *.jpg,
with $f set to each filename. 
${f%.*} is a form of parameter expansion that removes the filename extension
(string matching .*) from the end (i.e., the right side) of the $f filename.


Naming the output files .jpeg instead of .jpg is a good idea,
to prevent the *.jpg wildcard from matching them. 
Another approach is to write the output files to a different directory.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...