Unortunately, I am forced to use windows. So, I installed Cygwin to use some Linux commands.

The following command works fine. It replaces an image with its trimmed version.

"C:\Program Files\Cygwin\bin\convert" image1.png -trim image1.png 

However, how can I run this command on all image file?

"C:\Program Files\Cygwin\bin\convert" * -trim ????
  • Cygwin is Gnu/Linux with Linux replaced with a dll. Therefore it is a variant of the Gnu system, but not Linux. It is however still Unix. – ctrl-alt-delor Aug 17 '15 at 5:32
up vote 6 down vote accepted

You installed cygwin, so you can just use its shell for maximum command support : "C:\Program Files\Cygwin\cygwin.bat
this will give you a bash shell Then you can change directoty to go to the images location. Suppose your image location is "D:\Your Name\Images", to go there type cd "/cygdrive/d/Your Name/Images" and then call your command using the bash for loop :

for file in *
convert "$file" -trim "$file"
  • because * is done by the shell, not by each and every command. That is why your command did not work. On Microsoft's dos the command line interpreter is more primitive, and each command interprets the *. – ctrl-alt-delor Aug 17 '15 at 5:35
  • Fun thing is, when you're going with the for loop, you can do the exact same thing in cmd: for %f in (*) do convert "%f" -trim "%f". – Joey Aug 17 '15 at 11:56

Consider this more of an expansion/enhancement to @Slyx's answer.

Cygwin has a useful utility cygpath to convert Windows path to *nix-style understood within Cygwin's bash shell:

$ cygpath "D:\Path\To\Images"

Instead of the explicit for-loop suggested, you can also consider using find that has better support for filtering filenames and arguably safer support for names with spaces (as you may encounter with Windows):

find "$(cygpath "D:\Path\To\Images")"/ -type f -name '*.png' -exec convert '{}' -trim '{}' \;
  1. Inside the directory D:\Path\To\Images,
  2. Find files (-type f) ending with png (-name '*.png'),
  3. And for each of them, exec the convert command with '{}' as (quoted) placeholders for each resulting file.

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.