First of all, it is important to remember that it is not Terminal that executes the commands, but the shell that runs inside it. On a reasonably recently created user account on a Mac, that shell will be zsh.
If you’re running zsh, it should be possible to list all files that do not match the pattern image-*.jpg
using
setopt extended_glob
echo **/^image-*.jpg(.)
**
matches the current working directory and any subdirectories, ^
inverts the following pattern, and (.)
limits the whole thing to match only ordinary files (and not, for example, directories). For details, see the Filename Generation section of the zsh manual, or run man zshexpn
.
If the list looks ok, then re-run the second command but with rm
instead of echo
.
WARNING 1: It is easy to slip up with these things, and I cannot promise that I have not accidentally given you a command that wipes something you did not intend to. Please check the output of the echo
command carefully. Making a backup, as suggested by another answer, isn’t a bad idea either.
WARNING 2: If there are too many files, this will exceed the maximum allowed command size, and fail. In that case, the find
solution given in another answer is probably the way to go.