Subject: bug#14189: ls -d bug ??

From: Eric Blake <[email-masked]>
Date: Thu, 11 Apr 2013 15:31:15 -0600

On 04/11/2013 03:13 PM, Bob Proulx wrote:
> 
> If you didn't want it to list only the name of the directory and not
> the contents then why did you use the -d option?  Since -d
> specifically prevents it from listing the contents.
> 
>> ls -d, I would think, would tell you the same data that ls would tell you, minus
>> the individual files.
>> (In  other words - show all the data with a "d" in the permissions, but not show
>> the ones that don't have a "d" in the permissions).
> 
> Perhaps you want this?
> 
>   $ ls -log | grep ^d
>   $ ls -log | grep -v ^d
> 
> Or one of these:
> 
>   $ find . -maxdepth 1 -type d -ls
>   $ find . -maxdepth 1 -type d -exec ls -logd {} +

Or you could use this to approximate things:

$ ls -d */

the trailing slash forces the shell to filter out non-directories as
part of expanding the glob, and then list just the names instead of the
contents of all remaining directories.  But as written that only lists
non-hidden directories.  If you don't mind listing '.', you can get
closer with:

$ ls -d */ .*/

But for a full list of all subdirectory names excluding '.' and '..',
you need three globs; and either a shell option that suppresses a glob
that has no match, or ignoring the errors when ls tries to warn you when
a glob doesn't match:

Portable (but risks hiding errors):
$ ls -d */ .[!.]/ .??*/ 2>/dev/null

bash-specific:
$ (shopt -s nullglob; ls -d */ .[!.]/ .??*/)

All the sudden, the 'find' alternative suddenly seems nicer :)

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org