Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I convert images in a directory named foo to bar by like this.

$ mkdir bar
$ mogrify -path bar -negate foo/*.png

Is there option in Imagemagick that create a folder with -path option, if it not exist.

share|improve this question
mkdir -p /path/to/bar makes parents only as needed. – justbrowsing Sep 15 '13 at 22:13
I asked about option of mogrify not option of mkdir. – ironsand Sep 15 '13 at 22:20
up vote 2 down vote accepted


To take arguments from the mogrify command, and do other actions based on their content, you can "override" the program with a function of your own, then pass the original arguments on to the function:

   [[ $1 == "-path" ]] && [[ ! -d $2 ]] && mkdir "$2" 
   mogrify "$@"

To use it, just replace mymog wherever you would use mogrify:

mymog -path bar -negate foo/*.png

The function tests to see if the first argument is -path. If so, it goes on to test if the second argument is not an existing directory. If it is not, then it creates that directory. (The [[ ]] && is just another way to write if-then statements.) In either case it goes on to pass all the arguments to the mogrify command.

The only warning is that you have to put the -path argument first -- you can't stick it elsewhere in the line. You should be able to use this wherever you would normally use mogrify and it will just ignore the mkdir part if there is no -path definition.

To have this available in your daily usage, add those 4 lines to your .bash_profile or .bashrc file, depending on your operating system.

EDIT #2: New answer with no dependence on position of -path

function mogmod(){
    for ((i=0; i < $#; i++)) {
       if [[ ${args[$i]} = "-path" ]]
           [[ ! -d "$mypath" ]] && mkdir "$mypath" 
    mogrify "$@"
share|improve this answer
Thanks beroe! I can wrap mogrify like that. But I don't know how to wrap mogrify only when -path argument exists. It would be very helpful if you show me how. – ironsand Sep 16 '13 at 23:23
If you put testmake ; in front of mogrify -negate foo/*.png without the -path element, it will just make the directory if it doesn't exist, but the mogrify command will run independent of testmake. Basically, you can add that whenever you are using -path or omit it if not, but it shouldn't hurt anything to use it all the time. (Maybe I am not understanding your typical use.) – beroe Sep 17 '13 at 1:09
Thanks your advice. But I want to make directory only when the option -path is given. – ironsand Sep 17 '13 at 4:32
OK, I see what you want now and have updated the answer to do that. You probably would have gotten 5 replies in 5 minutes if you had tagged this as shell or bash with the title "How to re-use arguments from a shell command to perform other functions" ;^) – beroe Sep 17 '13 at 14:38
I'm really grateful that you write the function. But I don't use it if there is a condition that the -path must be the first argument. I know I'll forget the rule if I use the function once in a month. – ironsand Sep 18 '13 at 10:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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