0

I'd like to only the directory

$ ls -d 
.
#get . on macos

and the same on centos

[root@iz2ze9wve43n2nyuvmsfx5z ~]# ls -d .
.

I find the solution:

[root@iz2ze9wve43n2nyuvmsfx5z /]# ls -d */
bin/   dev/  home/  lib64/       media/  opt/   root/  sbin/  sys/  usr/
boot/  etc/  lib/   lost+found/  mnt/    proc/  run/   srv/   tmp/  var/

but the manual specify

   -d, --directory
          list directories themselves, not their contents

What's the reason -d perform this way?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
AbstProcDo
  • 2,453
  • 4
  • 23
  • 55
  • Possible duplicate of [why does ls -d also list files, and where is it documented?](https://unix.stackexchange.com/questions/75046/why-does-ls-d-also-list-files-and-where-is-it-documented) – Amanda Nov 01 '18 at 01:28

2 Answers2

1

ls -d applies to "this" directory, not to the directories contained in the location where you are running ls.

It seems odd if you're just running ls -d but if you want to know, say, who owns this directory, you could do ls -do and see only the owner of the current directory.

There's a much more comprehensive explanation included in the answer to why does ls -d also list files, and where is it documented?

And a lot of good insight at The result of ls * , ls ** and ls *** if you really want to go deep on ls

Amanda
  • 1,709
  • 2
  • 13
  • 26
  • `*` vs `**` is mostly about the expansions the shell supports, not about `ls` specifically, even though `ls` is used there in that question. – ilkkachu Nov 02 '18 at 14:10
0

The ls utility lists the pathname arguments given on its command line. If any of the arguments corresponds to a directory, then the complete contents of that directory will be displayed. If no argument is given, then ls lists the current directory, as if ls . was used.

To be able to list a directory (to see the directory entry for a directory), not its contents, you have the -d option of ls. With it, you can say ls -d dir and get the directory entry for dir and not its contents.

The reason ls -d by itself only returns a dot (the current directory) is that it's essentially the same as ls -d ., i.e. "give me the directory entry for the current directory without showing its contents".


What your ls -d */ command is doing is showing the directory entries for all directories in the current directory that does not have hidden names. You only get directories due to the / at the end of the pattern. Since you use -d, you don't get the contents of these directories.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • @AdwaitUpadhyay Thanks for your suggested edit, but `ls -d */` only shows directories because of the trailing `/` at the end of the pattern. Yes, `ls -d *` also shows directories, but it shows non-directories too, and it's furthermore not the command the user in the question is using in their example. – Kusalananda Aug 16 '22 at 09:29