2

I am trying to list all files with a name of 3 characters in /usr/bin.

ls ??? should do exactly that, but it lists every file in the directory. Note that ls ???? works as intended, showing only the files with a name of 4 characters. It also works with 1, 2, 5 and literally every number other than 3.

Steps to reproduce:

  • go to /usr/bin
  • type ls ???
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Mstrdav
  • 31
  • 3
  • This must be an interesting artifact of a filename in there, because that doesn't seem to happen for any other folder. – Ulrich Schwarz Nov 13 '21 at 15:25
  • 3
    ls -d ??? works as intended. – sudodus Nov 13 '21 at 15:30
  • which version of ls are you running? type ls and ls --version? The GNU one on the one box I tried doesn't follow the symlink with that command, and e.g. ls -l X11 shows the link, while ls -l X11/ shows the contents of the dir behind the link. (which is different for a regular directory) ls -H would change that, though. – ilkkachu Nov 13 '21 at 15:35
  • @ilkkachu, I tested with ls (GNU coreutils) 8.28 and it is subject to this behaviour. Please notice that ls -l ??? works OK for me. – sudodus Nov 13 '21 at 15:39
  • 3
    Scroll up! You'll see a listing of the three-letter files, then a blank line, a line with X11: and then all the file names. – Gilles 'SO- stop being evil' Nov 13 '21 at 15:49
  • Related, if not a dupe: https://unix.stackexchange.com/questions/62660/the-result-of-ls-ls-and-ls – Kusalananda Nov 13 '21 at 16:59

1 Answers1

5

Oh, I found it: ls will list the contents of directories you pass it on the command line. At least on the Ubuntu I tried this on, there is /usr/bin/X11/ which is a symlink to /usr/bin/. So you do get the three-letter files first, and then you get all the contents of /usr/bin again, which scrolls out all the others. (Try ls ??? | less to see this.)

  • 5
    Consider using -d with ls when you give an explicit list of items to list to ls. If you just want the list, you may just as well use echo ??? or printf '%s\n' ???. – Kusalananda Nov 13 '21 at 17:02