logo
hero
Štefan Hosťovecký

Web Consultant & Specialist & Full Stack Developer

Resize multiple JPEG Images efficiently with a Shell Script

hero

Learn how to resize JPEG images in a directory using a shell script with find, xargs, and the convert command from ImageMagick. Get a concise explanation of the script and its usage.

find . -maxdepth 1 -iname "*.jpeg" | xargs -L1 -I{} convert -resize 30% "{}" _resized/"{}"

And here's an explanation of this shell script:

find . -maxdepth 1 -iname "*.jpeg":

This command uses the find utility to search for all files in the current directory (.) with a maximum search depth of 1 (-maxdepth 1) and with a case-insensitive name match to .jpeg (-iname ".jpeg"). The find utility returns a list of file paths matching the specified criteria.

| (pipe operator):

The pipe operator takes the output of the find command and passes it as input to the next command.

xargs -L1 -I{}:

This is the xargs utility, which is used to execute a command on each of the inputs passed to it. The -L1 option specifies that xargs should only take one input at a time, and the -I{} option is a placeholder that will be replaced by each input item in turn.

convert -resize 30% "{}" _resized/"{}":

This is the convert command from the ImageMagick package. The -resize 30% option specifies that the image should be resized to 30% of its original size, and "{}" is the placeholder that will be replaced with each input file path in turn. The output of the convert command is saved to a directory called _resized with the same name as the original file (_resized/"{}").

So, in summary, this script uses the find utility to search for all .jpeg files in the current directory, and then uses xargs and the convert command from ImageMagick to resize each of these files to 30% of their original size and save them in a directory called _resized.