If I understand your problem correctly, you want all those filenames with a d
, an o
and a g
, in any case (upper or lower). Radu Rădeanu gave you an answer that works for the letters occuring in the order d
-o
-g
. For example aaGaaaoaaDaa
will not be found. You could alter his solution using the six possible permutations as, e.g.,
ls *[dD]*[oO]*[gG]* *[oO]*[gG]*[dD]* *[gG]*[dD]*[oO]* *[oO]*[dD]*[gG]* *[dD]*[gG]*[oO]* *[gG]*[oO]*[dD]*
(setting shopt -s nullglob
before is a good idea).
This becomes a bit messy, so you might want to also use shopt -s nocaseglob
so that the match is performed without regard to the case of alphabetic characters (quoted from the Bash Reference Manual) as:
$ shopt -s nullglob nocaseglob
$ ls *d*o*g* *o*g*d* *g*d*o* *o*d*g* *d*g*o* *g*o*d*
Another option is to use the find command as, e.g.,
$ find . -maxdepth 1 -iname '*d*' -iname '*o*' -iname '*g*' -type f
The -maxdepth 1
option is to limit to the current directory and not its subdirectories and -type f
to limit the search to regular files. Change these options to fit your needs. -iname
is for a case-insensitive search on the name (using glob patterns). Don't forget the quotes!
One advantage of find is that you'll be able to -exec
stuff if you need to perform any operation on these files, e.g., renaming them, or appending stuff to them, etc. Another advantage is that if you have a huge number of such files, the globbing will take ages, and might even overflow the maximum number of arguments allowed. find will be alright, whatever the number of files is.
Hope this helps!
Edit. It seems I missed the in this order part in your title. Hahaha. Well, then the find command would simply be:
$ find . -maxdepth 1 -iname '*d*o*g*' -type f
and the ls
/printf
solution would be:
$ shopt -s nullglob nocaseglob
$ ls *d*o*g*
$ printf '%s\n' *d*o*g*
ls *[dD]*[oO]*[gG]*
is not good enough?