Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a mix of different files in my folder and I thought I can simply do:

mv '*.(png|jpg|gif)' images/

to have them moved to the image folder. But it doesn't work... any thoughts?

share|improve this question

migrated from stackoverflow.com May 27 '11 at 22:13

This question came from our site for professional and enthusiast programmers.

    
I think your error is simply that you have single quotes around the file matching pattern. You will need double quotes, or no quotes at all, in order to use glob expansion. –  Caleb May 28 '11 at 8:13

3 Answers 3

Use a feature in bash called brace expansion

mv *.{png,gif,jpg} images
share|improve this answer
mv *.png *.jpg *.gif images/

should do it

share|improve this answer

And what shell are you using?

Goxu has the correct answer. There's no reason you should do something more esoteric if you can do it in a more simple and understandable manner.

In the end, the shell expands the command to include all the files before passing the list to the mv command, so there's really no performance gain or loss by doing it differently.

However, if you want to be Mister Fancy Pants, you should know that both Bash and Kornshell (both available on OS X) can do the following using extended glob expressions:

$ mv *(*.png|*.jpg|*.gif) images

Extended glob expressions (aka pattern matching) is really helpful when you want to remove everything from a directory except for one file. For example, you expanded a tar archive, and want to delete all the files except for the tar archive:

$ rm -rf !(foo.tar.gz)

The thing with Bash is that you have to turn on this functionality via the shopt command first:

$ shopt -s extglob

Take a look at the Bash manpage and look at the section on Pattern Matching. It contains a lot of neat shell tricks that not too many people know about. It gives your shell some secret super powers, but always remember to use those powers to help mankind and never for evil.

share|improve this answer
    
*.@(png|jpg|gif) is a bit simpler. zsh also supports these ksh patterns with its KSH_GLOB option, and/or its own equivalents with its EXTENDED_GLOB option (*.(png|jpg|gif)). –  Chris Johnsen May 28 '11 at 3:30

Your Answer

 
discard

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