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?

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

  • * vs ** is mostly about the expansions the shell supports, not about ls specifically, even though ls is used there in that question. – ilkkachu Nov 2 '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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.