3

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 ?

4

1 回答 1

5

要查找其中包含新行的所有文件和目录,您可以使用此 POSIX 兼容调用find

find . -name '*
*'

其中文字换行符嵌入在单引号中。bash支持附加语法来指定换行符:

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

或者不那么笨拙:

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

或使用-regex

find . -regex ".*$EOL.*"
于 2014-02-12T11:44:47.153 回答