Mindaugas Kubilius
Mindaugas Kubilius

Reputation: 341

How to find files containing newlines in their names

There is so much told about correct handling of file names which contain weird symbols like newlines. I thought using IFS set to newline would solve the issue in general except obviously for the case if one has a name with newline in it. So as a means of defense script could check at first whether a subtree has at least one file with newline and stop with a message to fix the name first. Surprisingly I couldn't make find doing that with -regex match. Only way successful was a rather ugly

find . -name "*"$'\n'"*"

But for example find . -regex ".*\n.*" does not work. Emacs regex should allow escape characters like \n, shouldn't it ? Interestingly that it matches another file with character n in the name. Experimented with different -regextypes only to find that types awk sed posix-extended and some more would match file with a newline (let's say a\nxxx) but in addition they will match files with character n as well. Weird. On other hand GNU find documentation does not tell anything about support of escaped characters like \n. Are they really not supported so we can't use \t \n \r \a and similar in find regexps ?

Upvotes: 4

Views: 1954

Answers (1)

anubhava
anubhava

Reputation: 785266

To find all file and directories that have new line in them, you can use this POSIX-compatible call to find:

find . -name '*
*'

where a literal newline is embedded in the single quotes. bash supports additional syntax to specify a newline:

find . -name \*$'\n'\*

OR little less clumsy:

EOL=$'\n'
find . -name "*$EOL*"

Or using -regex:

find . -regex ".*$EOL.*"

Upvotes: 6

Related Questions