Bash – How to find all files in a directory with illegal characters in the file name

bashcommand-line-interfaceposix

Here is what I prepared and where I got stuck:

find / \! -name "*[:alnum:]*" -type f -ls > ~/Desktop/files_not-allowed-char.txt

I actually want to list all files which include one of these characters:

\ / : * ? " < > |

The other way around: I want to list all files which have other characters in the name than:

[A-Z][a-z][0-9]äöüÄÖÜß_-.()#[]
or in other "words":
[:alnum:] and "äöüÄÖÜß_-.()#[]"

I hope I could clarify my wishes. I am entering unknown territory…

Thanks in advance!


Background info:
I want to change from AFP to SMB but have to make sure that I rename all invalid file and folder names (e.g. invalid_Directory_Name./ ).

Best Answer

Using ! in front of "[:alnum:]" will negate any file that has a character in [:alnum:] so it will only match file names which ONLY have non alphanumeric characters.

You could use a different regex to find punctuation find . -name "*[:punct:]" but that includes characters you don't care about. find . -name "*[\/:*?"<>|]*" should match anything with at least one of those characters in the brackets. (though you probably need to escape a few of them :) )