-3

Why doesn't the command:
ls -ad /*

...show both hidden and non-hidden directories? And given this does not work, what would be the simplest command for showing hidden and non-hidden directories, without showing files?

Thanks!

1 Answers1

1

/* is expanded by the shell before ls ever runs. The expansion only includes non-hidden files unless the dotglob option is set. Also, if you want the expansion limited to directories, use /*/ instead.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks! Note, "/*" was a typo, I meant to type "*/" but what you're saying makes sense. As for the second part of my question, what's the most straightforward way of listing all directories, hidden and non-hidden, and only directories? – Kirk Sealls Feb 01 '22 at 03:38
  • Probably `shopt -s dotglob; ls -d */`. If you want `.` and `..` as well, use ` ls -Ad */`, as the glob will not match them either way. – chepner Feb 01 '22 at 15:15
  • Thanks again, [@chepner](https://stackoverflow.com/users/1126841/chepner)! One note, neither of these commands are including `.` or `..`: 1) `ls -Ad */` 2) ...or `ls -ad */` – Kirk Sealls Feb 04 '22 at 19:11
  • Oh, right, because `ls` only showing whatever files the expansion of `*/` indicates. `-a` and `-A` only affect which files `ls` generates itself. – chepner Feb 04 '22 at 19:13