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.

Windows (Command Prompt and PowerShell)

Windows handles quoting slightly differently, especially when dealing with batch scripts.

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"
done

Windows Command Prompt Loop Example

for %i in (* *.jpg) do convert "%i" "%~ni.png"

Note: For newer versions of ImageMagick (v7 and above), the convert command has been replaced by magick. The quoting and escaping syntax for spaces remains exactly the same: magick "input file.jpg" "output file.png".