8

When navigating to /usr/local/bin a file called "R" can be seen:

enter image description here

But when cd /usr/local/bin and ls (or ls -a), we see a long list of files, but no file called R:

enter image description here

I was surprised by this - apparently finder can see files that ls cannot. Why doesn't ls -a show all files that finder does?

5
  • 9
    Is that the whole list? It's important to note that sorting in Finder is different than the sorting in Bash/Zsh. Issue the command ls -ad R* and let me know what you get.
    – Allan
    Commented Aug 17, 2020 at 0:43
  • 1
    @Allan ah. I get R Redirect Rscript.
    – stevec
    Commented Aug 17, 2020 at 0:45
  • 1
    I'll write up an answer....
    – Allan
    Commented Aug 17, 2020 at 0:46
  • Wouldn't ls -1 | sort -f give you the same results as the finder?
    – fd0
    Commented Aug 17, 2020 at 17:33
  • @fd0 You're right. I'll use ls -1 | sort -f in future rather than ls
    – stevec
    Commented Aug 17, 2020 at 17:37

1 Answer 1

20

It's there, but you're not seeing it because it's not where you're expecting it to be because the sorting in Finder is different than the sorting in Bash/Zsh. Basically Finder does a case insensitive search whereas Bash is case sensitive. For example, "a" follows "Z"

If you're looking for a specific file and you know the case, you can use a little ls magic to find it...

$ ls -Ad R*

Where...

  • -A = List all files, except . and ..
  • -d = Treat directories as files and do not search them recursively
  • R* = All files that begin with the letter "R"
5
  • 4
    Not saying this is wrong, as the upvotes and acceptance show that, but I just don't understand. Surely if it's just a case of sorting the list differently, then in bash you'd expect to see the file at the top of the list. But it looks to me like it's not there at all?
    – Tom Bowen
    Commented Aug 17, 2020 at 12:33
  • 13
    I think the OP has just shown part of the output.
    – Gremlin
    Commented Aug 17, 2020 at 13:55
  • 3
    @TomBowen In the OP, I cropped the list to show only a few files, as there were ls -1 | wc -l = 1047 files in total. Interesting, only 26 of them started with either a number or an upper case letter (which is why I missed some of those files, including "R").
    – stevec
    Commented Aug 17, 2020 at 15:40
  • 4
    "Basically Finder does a case insensitive search whereas Bash is case sensitive." - This has nothing to do with bash though; it's ls which decides to sort its output that way (likely in response to locale settings), the shell is irrelevant in this behaviour.
    – marcelm
    Commented Aug 18, 2020 at 6:30
  • 1
    @marcelm Yes, ls uses LC_COLLATE to determine the collation, usually using something like readdir/scandir + setlocale at program startup.
    – ljrk
    Commented Aug 18, 2020 at 12:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .