1

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 wildcard 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?

0

1 Answer 1

3

I don't think the regexp engine parses hex escape sequences. Try putting a literal CR character in the regexp, using $'...'

find . -name $'Icon\r'

There's no need to use a regular expression, since you're just looking for a specific name, not a pattern.

10
  • The same also appears to work with the -regex option. Weird, but good enough for my needs. Also, if you could please upvote the question - some bozo keeps downvoting every question I ask on SO. Commented May 29 at 22:32
  • 2
    To be fair, this isn't really a programming question, so the downvote may be deserved.
    – Barmar
    Commented May 29 at 22:37
  • good point - it used to be okay to ask shell questions on SO, but nowadays The unix specific subsites are probably the better choice. However, I use it in a program, so it's a programming issue for me :) Plus, downvotes shall be explained in a comment. I think this is just general vandalism (either against me or against Mac specific questions) Commented May 29 at 22:39
  • What still bugs me is that, according to man find, the \x notation should work. I wonder if it needs special escaping when using it in a shell. Commented May 29 at 22:44
  • 1
    @tripleee It makes reference to the re_format(7) man page, which does mention it.
    – Barmar
    Commented May 31 at 16:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.