I have some files in a directory that are named
IMG_9999.jpg. I want to move to another directory the files that lexicographically have a name greater than
IMG_9431.jpg How can I do that?
You can use
Here is the magic:
And the desired result:
With brace expansion, which is available in Bash 3 and above, as well as Zsh and several other shells:*
The braces will be expanded to all the numbers between 9431 and 9999, so it is equivalent to writing out this:
This will fail if there are too many files (see this post about
If your shell lacks brace expansion features, or you have too many files, you can do this – which could be a little slower:
If you really want to sort lexicographically, have a look at Ярослав Рахматуллин's answer. Much nicer than what follows here.
You'll have to sort your file names somehow. This is what I came up real quick, and it's not pretty and far from ideal. It works with GNU/Linux (
This would be easier if we didn't have to deal with files that contain quotes or whitespace, but… that's just me. Better be safe than sorry.