Search code examples
regexbashfind

Bash command `find` with Regex pattern doesn't match correctly non-word-characters


I'm writing a Bash script and need to match all files and folders inside a directory which contain non-word-characters, uppercase-characters and German Umlauts. Inside my test directory I tried different combinations, but either I match any test file/folder or none despite the fact that it is working at Regex101 (example 1, example 2). I can't figure out if the problem are tokens/character classes i use or something Bash-specific:

$ ls 
kukukae.txt   KuKuKä.txt  'Replüce-?!% Test.txt.bak'   repluece_test.txt.bak  'Sä=?)(&%$§"!°^`´+*~#'   somo_oemo.gif  'Somo Ömo.gif'   super_  'why?that' # some example files and one folder
$ find . -maxdepth 1 -regex '[^\w\.\/]' # nothing matched
$ find . -maxdepth 1 -regex '.*[^\w\.\/].*' # matches also files without non-word-characters
./super_
./kukukae.txt
./Sä=?)(&%$§!°^`´+*~#
./somo_oemo.gif
./repluece_test.txt.bak
./why?that
./KuKuKä.txt
./Replüce-?!% Test.txt.bak
./Somo Ömo.gif
$ find . -maxdepth 1 -regex '[[:upper:]]|[[:space:]]|[\?\&\ä\ü\ö\!\"\§\$\\\(\)\[\]\}\{\=\`\´\+\*\~\#\:\;\,\^\°\<\>\|]' # again nothing
$ find . -maxdepth 1 -regex '.*[[:upper:]]|[[:space:]]|[\?\&\ä\ü\ö\!\"\§\$\\\(\)\[\]\}\{\=\`\´\+\*\~\#\:\;\,\^\°\<\>\|].*' # nothing one more time

It should look like the following, where only the files with non-word-characters and Umlauts are matched:

$ find [working command]
./Sä=?)(&%$§!°^`´+*~#
./why?that
./KuKuKä.txt
./Replüce-?!% Test.txt.bak
./Somo Ömo.gif

In both versions I don't want to match / or . intentionally ([^\w\.\/]), because it would include path and dots of file extensions.

Does any Regex-pro or Bash-veteran has an easy solution?

Thanks and best


Solution

  • I don't think you need a regex for this

    $ LC_ALL=C find . -name '*[!0-9a-z_.]*'
    ./Somo Ömo.gif
    ./Sä=?)(&%$§"!°^`´+*~#
    ./KuKuKä.txt
    ./Replüce-?!% Test.txt.bak
    ./why?that
    $