I have a folder with images(png, bmp, jpg) and extension-less files.

I can't figure out how to move only the extension-less files. They are all random numbered files.


With bash

shopt -s extglob
mv -- !(*.jpg|*.png|*.bmp) targetdir/
| improve this answer | |
  • The ! is to tell the command to do the opposite of what is specified? – Dominique May 2 '14 at 22:28
  • @Dominique, ! is an extended glob operator that matches anything other than the patterns specified, read more about it here – iruvar May 2 '14 at 22:35
  • 1
    Can I just suggest writing the target directory as targetdir/ to guard against the case where if it doesn't exist, the source files are successively renamed as targetdir? – steeldriver May 2 '14 at 22:44
  • @steeldriver, good point, incorporated. Feel free to edit anytime – iruvar May 2 '14 at 22:47
  • @steeldriver, if there are several sources, mv won't do anything if the target isn't a directory. But you are right in that it won't work right if there is just one source – vonbrand May 3 '14 at 8:59

Using the !(...) ksh extended glob operator for negation, so in ksh, zsh -o kshglob or bash -O extglob:

rm -- !(*.*) /dest

Would move the files whose name doesn't contain a dot (note that in shells other than zsh, if there's no such file, it could still move the file called !(*.*) even though that file does contain a dot).

Using zsh's ^ extended glob operator for negation, so in zsh -o extendedglob:

rm -- ^*.* /dest

In all the above, hidden files would not be moved, first because their name does contain a dot (they start with a dot), but also because hidden files are excluded from globs unless the glob itself starts with a literal ..

If you want to also move .foo files but not .foo.png, you can do:

mv -- ^?*.*(D) /dest # with zsh -o extended-glob

mv -- !(?*.*) /dest # with bash -O extglob -O dotglob
                    # or   zsh -o kshglob -o dotglob -o nobareglobqual
                    # or   env FIGNORE='@(.|..)' ksh93

mv -- @(.|)+([^.]) /dest # with bash -O extglob or AT&T ksh
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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