Search code examples
bashls

Why does 'ls' recurse into folders?


Why does ls always recurse into folders when I use a wildcard on it (I'd rather that it didn't do this and instead just showed me all items in the directory starting with m and nothing else)?

$ ls
boot/  etc/   lost+found/  mnt/  proc/  run/  srv/  tmp/  var/  init*  lib32@  libx32@ dev/  home/  media/ opt/  root/  snap/  sys/  usr/  bin@  lib@   lib64@  sbin@

/ $ ls m*
media:

mnt:
c/  d/  e/  wsl/

$ alias ls
alias ls='ls -FAh --color=auto --group-directories-first'

Solution

  • Why does ls always recurse into folders when I use a wildcard on it

    It's according to the specifications if the wildcard globbing matches a directory.
    From The Open Group Base Specifications Issue 7, 2018 edition:

    For each operand that names a file of type directory, ls shall write the names of files contained within the directory as well as any requested, associated information.

    You can however override this default behavior by using the -d option:

    Do not follow symbolic links named as operands unless the -H or -L options are specified. Do not treat directories differently than other types of files. The use of -d with -R or -f produces unspecified results.