0

I have standard ls installed, I also have lsd installed, which is a nerd-font drop-in replacement for ls. I mention this because the error may have something to do with the alias.

For testing right now, I have alias ls=ls in place to remove any of my custom commands.

If I type ls in the /etc directory, where there are obviously several subfolders (which you can see without the -d flag), I get the following:

>>>root@Unraid:/etc# ls -d /
/
>>>root@Unraid:/etc# ls -d
.
>>>root@Unraid:/etc# lsd -d
📂 ./
>>>root@Unraid:/etc# lsd -d /
📂 //

the missing codepoint is just a folder 📂 icon. I replaced all the boxes with browser compatible emojis just for clarity. LSD has been installed for a few days and isn't the issue. They behave identically.

I prefixed the prompt lines with >>> just to make them easier to pick out.

Let's use my home directory as an example. The correct response is that there are 2 folders in ~, lsd and pkg

>>>root@Unraid:~# lsd
📂 lsd/  📂 pkg/  📃 appdataUNRAID.code-workspace@  📄  mdcmd@

>>>root@Unraid:~# ls -F
appdataUNRAID.code-workspace@  lsd/  mdcmd@  pkg/


>>>root@Unraid:~# lsd -F
  📂 lsd/    📂 pkg/  📃  appdataUNRAID.code-workspace@  📄  mdcmd@
>>>root@Unraid:~# ls
appdataUNRAID.code-workspace  lsd  mdcmd  pkg
>>>root@Unraid:~# ls -lhp
total 0
lrwxrwxrwx 1 root root  30 Mar 14 16:41 appdataUNRAID.code-workspace -> ./appdataUNRAID.code-workspace
drwxrwxrwx 3 root root 140 Mar 18 11:35 lsd/
lrwxrwxrwx 1 root root  21 Mar 13 18:55 mdcmd -> /usr/local/sbin/mdcmd
drwxrwxrwx 2 root root 140 Mar 24 10:10 pkg/

Again these icons are missing here, but the single directory in here "lsd" has the correct folder icon. Both these commands take appropriate flags.

Here we can see that they take flags just fine, and they find the two directories perfectly fine, but TWO implementations of ls only return the . directory on the -d flag, even when combined with other flags.

>>> root@Unraid:~# lsd -d
  📂 ./

root@Unraid:~# ls -dFl
drwx--x--- 11 root root 420 Mar 24 10:10 ./


## The full text flag also doesn't work.
root@Unraid:~# ls  --directory
.

It doesn't matter what other flags are in place, if the D flag is in then two separate implementations of ls only show the current directory .,

I mean, it doesn't even show the default .. 2nd directory that's there. I tried adding recursion as well.

What's happening?

6
  • 3
    It's unclear what you expect to happen when you use the -d option with ls. It's also unclear what you think the use of this option is.
    – Kusalananda
    Commented Mar 24, 2022 at 18:18
  • Perhaps you were looking for ls -d */, perhaps not.
    – hobbs
    Commented Mar 25, 2022 at 1:40
  • If you're not entirely sure what's running, try which ... which ls
    – Basic
    Commented Mar 26, 2022 at 2:35
  • I expect it to list subdirectories. am I going nuts or is this not what it should do? I am not new to cli linux nor do i claim to be expert in it.
    – Andy T
    Commented Mar 27, 2022 at 4:30
  • This question is similar to: What is the meaning of ls -d?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.
    – muru
    Commented Aug 12 at 23:58

3 Answers 3

20

When you provide the -d option, you're telling ls that you don't want it to list the contents of any directories, only the directory name itself. When you don't provide any additional arguments to ls, the default is to list the current directory. As a result, your variations on ls -d all show the right thing -- the name of the current directory: .

7

Jeff Schaller ♦s answer explains how/why the -d flag behaves this way. In this answer, I'd like to expand on why or when this is useful.

ls -d without additional arguments is rarely useful. With only one argument, I don't find it very useful either. With ls -ld dirname you get some useful information such as permissions, ownership, and timestamp, but you can also get that with stat dirname.

Where ls -d shines is with wildcards.

Compare:

$ ls -l */*
one/a:
total 0
-rw-rw-r-- 1 gholl o16217 0 Mar 25 10:20 aa

one/b:
total 0
-rw-rw-r-- 1 gholl o16217 0 Mar 25 10:20 bb

two/c:
total 0
-rw-rw-r-- 1 gholl o16217 0 Mar 25 10:20 ab

two/d:
total 0
-rw-rw-r-- 1 gholl o16217 0 Mar 25 10:20 oe

with:

$ ls -ld */*
drwxrwxr-x 2 gholl o16217 4096 Mar 25 10:20 one/a
drwxrwxr-x 2 gholl o16217 4096 Mar 25 10:20 one/b
drwxrwxr-x 2 gholl o16217 4096 Mar 25 10:20 two/c
drwxrwxr-x 2 gholl o16217 4096 Mar 25 10:20 two/d

or

$ ls -l */
one/:
total 8
drwxrwxr-x 2 gholl o16217 4096 Mar 25 10:20 a
drwxrwxr-x 2 gholl o16217 4096 Mar 25 10:20 b

two/:
total 8
drwxrwxr-x 2 gholl o16217 4096 Mar 25 10:20 c
drwxrwxr-x 2 gholl o16217 4096 Mar 25 10:20 d

or

$ ls -ld */*[ac]
drwxrwxr-x 2 gholl o16217 4096 Mar 25 10:20 one/a
drwxrwxr-x 2 gholl o16217 4096 Mar 25 10:20 two/c

When I'm using wildcards, usually I do not want to descend into directories. The ls -l */* alternative does not give me much information about the directories one/a, one/b, two/c, or two/d, whereas ls -ld */* does. The alternative ls -l */ does too, but in a different way, and usually I prefer the more compact output of ls -ld */*; and of course, the wildcards allow me to match only some names, whereas ls -l */ always lists all contents of the subdirectories. The result of ls -ld */*[ac] can only be obtained with the -d flag.

  • I almost always use the -d flag when I pass wildcards to ls.
  • I rarely use the -d flag it when I pass a single directory.
  • I never use the -d flag when I pass no arguments at all.
3
  • TL;DR never use -d either ls -ld . or ls -ld *foo* when you don't know if foo is file or dir.
    – Archemar
    Commented Mar 25, 2022 at 9:46
  • ls -dl dirname is useful for finding the owner & permissions on the directory. I use this invocation quite frequently.
    – doneal24
    Commented Mar 25, 2022 at 12:30
  • @doneal24 True. I tend to use stat in this case.
    – gerrit
    Commented Mar 25, 2022 at 13:21
1

By default ls will show information about each given name, except any that are directories. If any directories are given, it will scan each to get a list of names, and then show information about those instead.

However the -d option suppresses this, causing ls not to treat directories specially, and just show information about all names as given.

If ls is invoked with no pathname arguments, it will behave if . is given. In the absence of -d this will show the contents of the current directory, but with -d it will simply report on . -- as you've observed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .