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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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:]]*'
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
-regex (also supported by FreeBSD). If the pathname has an embedded newline (rarely done, but used as a frequent counterexample), POSIX
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.