Sample Header Ad - 728x90

How to find file names with non-printable characters using BSD `find -regex`?

1 vote
1 answer
205 views
Some folders on macOS have custom icons that are stored in a file named Icon?, where the ? is actually a CR character, and only prints as "?" in most cases (in Terminal and Finder). But when printing such a file name in hex in Terminal, you'll get:
$ ls -l1 Icon* | xxd
00000000: 4963 6f6e 0d0a         Icon..
The 0d is the CR at the end of the name, and the 0a is the LF that's printed by ls at the end of each line. Now, I like to find such files, using find. I'd think that this would be the way: find -E . -iregex '.*/Icon\x0d' Nor does: find -E . -iregex '.*/Icon\r' However, this won't find it. But this finds it (using . as a whildcard char): find -E . -iregex '.*/Icon.' But something is wrong with looking for hex chars in general, because this doesn't work either: find -E . -iregex ".*/\x49con." \x49 is the code for I, so this should work. So, if you want to try this yourself, take _any_ file and try to find it using the find command with the -regex option and specifying at least one character in hex, e.g. looking for the file named 'a' with the regex \x61 or whatever is correct. Can you accomplish it? Note, on SO, where I had asked first, someone suggester to use this form: find -E . -iregex $'.*/Icon\r' That does indeed work, but it's not really what I want, because I use construct this command in a program where I let the user enter a regex pattern and then apply a regex function to check if a file name matches the pattern. I like to use the very same pattern when invoking the find command, and therefore I'd prefer it if I could use the very same pattern in both places. Using the $'…' wrapping might change the interpretation of regex patterns (not sure if that'll actually cause problems), so I rather would like to figure out why I cannot use the \x.. notation in the command, because according to man find and man re_format this should work.
Asked by Thomas Tempelmann (133 rep)
May 29, 2024, 11:00 PM
Last activity: May 30, 2024, 06:46 AM