How to Use ImageMagick Convert with Spaces in File Names
Handling file names that contain spaces when using the ImageMagick
convert command requires proper escaping or quoting to
prevent the command line from treating the spaces as argument
separators. This article provides a quick overview of the syntax needed
to process these files on various operating systems, including practical
examples for both single files and batch processing.
The Standard Quoting Syntax
The most reliable way to handle spaces in file names across most
command-line interfaces is to wrap the entire file path in
double quotes ("). This tells the system
to treat the enclosed string as a single argument.
For example, if you have an image named
my vacation photo.jpg and want to convert it to a PNG, use
the following syntax:
convert "my vacation photo.jpg" "output photo.png"
Platform-Specific Approaches
Depending on your operating system, you have a few different options for formatting your commands.
Linux and macOS (Bash/Zsh)
On Unix-like systems, you can use either double quotes, single quotes, or backslash escaping.
- Double Quotes:
convert "summer bash.jpg" -resize 50% "summer bash mini.jpg" - Single Quotes:
convert 'summer bash.jpg' -resize 50% 'summer bash mini.jpg' - Backslash Escaping: You can place a backslash
(
\) directly before each space to escape it.convert summer\ bash.jpg -resize 50% summer\ bash\ mini.jpg
Windows (Command Prompt and PowerShell)
Windows handles quoting slightly differently, especially when dealing with batch scripts.
- Command Prompt (cmd): Always use double quotes.
Single quotes will result in a syntax error.
convert "product image.png" -flip "product image flipped.png" - PowerShell: You can use both single and double quotes. If your file path contains a variable, use double quotes so the variable resolves correctly.
Batch Processing Files with Spaces
When looping through a directory of images where multiple files contain spaces, your loop variables must also be quoted.
Bash Loop Example
for file in *" "*.jpg; do
convert "$file" "${file%.jpg}.png"
doneWindows Command Prompt Loop Example
for %i in (* *.jpg) do convert "%i" "%~ni.png"Note: For newer versions of ImageMagick (v7 and above), the
convertcommand has been replaced bymagick. The quoting and escaping syntax for spaces remains exactly the same:magick "input file.jpg" "output file.png".