How do I find files that have ~ * and other special characters in the name?


find . -name "*\*"

should match "any characters" and then *, but it matches nothing; how can I get the command to correctly match the files?

  • 2
    What is your OS? – cuonglm May 4 '16 at 8:30
  • Please edit your question and i) tell us what operating system you are using. There are significant differences between different implementations of find. ii) Show us the files it was supposed to find and didn't. iii) Tell us the shell you are using to run this from (is that bash?). – terdon May 4 '16 at 8:47
  • When you edit, please also clarify what you mean by "other special characters". We have no idea what you consider to be 'special'... – Toby Speight May 4 '16 at 11:08

Implementations of find vary, but they should all handle character classes in wildcards (POSIX.2, section 3.13):

find . -name '*[~*]*'

If newline is among your "special" characters, you may need to work out how to get your shell to pass it to find. In Bash, you can use

find . -name $'*[\t \n]*'

to show files containing whitespace, for example. A simpler method, if supported, is to use a character class:

find . -name '*[[:space:]]*'
  • 1
    The POSIX character class [[:space:]] should work. – glenn jackman May 4 '16 at 12:27

If you want something more general than matching a specific character, you would have to use regular expressions. Since the question is not tagged "linux", the proper answer would use POSIX:

find . | grep '[*~]'

If you want to make it Linux-specific, you can use the GNU find option -regex (also supported by FreeBSD). If the pathname has an embedded newline (rarely done, but used as a frequent counterexample), POSIX find+grep will not work. But with the -regex extension, this "works" to print names which have embedded newlines:

find . -regextype posix-awk -regex '.*[*~]'

although the manner in which find is used is not part of the question.

Further reading:

  • 1
    this does not match file with name "~*" – trolkura May 4 '16 at 8:32
  • @ThomasDickey: This fails if file name contains newline. – cuonglm May 4 '16 at 8:43

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.