Subject: bug#21218: ls -d

From: "Sneeh, Eddie" <[email-masked]>
Date: Mon, 10 Aug 2015 12:09:21 -0700

Hi Eric, thanks for the quick response and clarification.  I wasn't
completely clear what -d was supposed to do exactly.  The man pages define
ls -d as:
*"list directory entries instead of contents".*
The files in the target directory are contents of that directory, but they
get listed anyway!   I still think ls -d is not accurate.   Here are some
examples:
% \ls -d f1
f1
(but f1 is not a "directory entry" so this output is not consistent with
the definition above.)

If:
% \ls /tmp/mydir
d1/ d2/ f1 f2
% \ls -d .
.
(but where are the rest of the directories?)
% \ls -d /tmp/mydir
/tmp/mydir
(Same thing, no listing of directory entries in that directory as the
definition says)
The behavior I'm seeing is:  list directory entries instead of contents, *but
not including the parent directory (or the directory specified as argument)*.
I guess that's where the confusion is.  IMHO, the directory that the user
specifies should be the first to follow the rule, because that's what the
user is concerned about the most.
% \ls -d *
can be accomplished by a simple:
% \ls
% \ls -d <directory I'm concerned about)
just does a pwd!

Thanks for the find command.  That's what most of us have been doing, but I
feel that listing directories/files/links should be an ls function more so
than a find's, and it should be a simple command rather than a long one.
If it were up to me, this is what I would do:
% ls -f  (list files)
% ls -l (list links)
% ls -d (list directories)
This is more intuitive and useful, at least to me.
The man pages for ls -f say: "do not sort"  I'm not sure how -f was derived
from that.
Since these switches are already taken to do other things, what do you
think about this:
% ls -fo (list files only)
% ls -lo (list links only)
% ls -do (list directories only)

For now, I like your solution: ls -d */   which I'll be using instead of
the find command.  I'll have to "tolerate" the extra slash at the end, but
I like this better than other options.   Thanks again.

Eddie

On Fri, Aug 7, 2015 at 8:43 PM, Eric Blake <[email-masked]> wrote:

> tag 21218 notabug
> thanks
>
> On 08/07/2015 06:01 PM, Sneeh, Eddie wrote:
> > Hi,
> > I believe there is a problem with ls -d (unless the intent is to just
> list
> > 1 directory).
>
> Thanks for the report.  However, I think you have misunderstood the
> purpose of -d.  Try this for comparison:
>
> touch file
> ls . file
> ls -d . file
>
> All -d does is to list command line arguments that are directories as
> the name of the directory itself, rather than descending and showing the
> directory's contents.
>
> > Say I have a dir mydir, it has directories d1 and d2, and files f1 and
> f2.
> > I want to list only d1 and d2.  How do I do that?  I tried ls -d but that
> > doesn't seem to work:
> > \ls -d mydir
> > returns: mydir
> > \ls -d mydir/*
> > returns: mydir/d1 mydir/d2 mydir/f1 mydir/f2
> > but f1 and f2 are files.
>
> If you want to list ONLY directories, use a glob that guarantees only
> directories can be matched:
>
> ls -d mydir/*/
>
> The trailing slash can only match directories, and will therefore skip
> ordinary files.  Or, you can use find:
>
> find mydir -mindepth 1 -maxdepth 2 -type d
>
> I'm closing this as not a bug as ls is working as designed, but feel
> free to ask further questions.
>
> --
> Eric Blake   eblake redhat com    +1-919-301-3266
> Libvirt virtualization library http://libvirt.org
>
>

-- 
Best Regards,
*Eddie Sneeh*