[Novalug] ls -d

Jon LaBadie novalugml@jgcomp.com
Sat Oct 15 13:50:19 EDT 2016


On Sat, Oct 15, 2016 at 09:53:35AM -0400, Michael Henry via Novalug wrote:
> On 10/15/2016 12:14 AM, Jon LaBadie via Novalug wrote:
> > On Fri, Oct 14, 2016 at 05:33:54PM -0400, Michael Henry via
> > Novalug wrote:
> >>
> >> [big snip]
> >>
> >> So for Gary's question "How do I use ls to just list the
> >> directory names in my current working directory (and nothing
> >> else)?", it's just ``ls -d */`` (shown below using ``/etc`` as
> >> the current working directory)::
> >>
> >>   $ cd /etc
> >>   $ ls -d */
> >>   acpi/                 ifplugd/                   python3.3/
> >>   alternatives/         ImageMagick-6/             python3.4/
> >>   [... lots of directories deleted ...]
> >>   gss/                  python2.7/                 xpdf/
> >>   gtk-2.0/              python3/                   zsh/
> >>   gtk-3.0/              python3.1/
> >>   hp/                   python3.2/
> >>
> > Not having known that trick<<<<<feature, I looked at the ksh
> > manpages.  This led to finding an interesting extension.
> > If the asterix is doubled the ksh does recursive matching
> > descending the directory tree.  So adding a second '*' to
> > Michael's command lists all directories, not just the top
> > level.
> >
> >   $ ls -d **/
> >
> > 8248 directories under my home directory.
> > No wonder I can't find anything.
> >
> > Two surprises, even "hidden" dot-name directories are matched
> > by ** though not by * and second, bash did not act the same
> > way as ksh; "ls -d */" and "ls -d **/" gave the same output,
> > just the top level.  My bash-foo is weak, maybe some option
> > or environment setting is needed.
> >
> > Jon
...
> 
> Bash does support that feature, but it's disabled by default.  I
> have the following in my ~/.bashrc to enable it, using the
> ``shopt`` (shell option) command to set (``-s``) the
> ``globstar`` option::
> 
>   # Use "**" in globs.
>   shopt -s globstar

Confirmed, setting the globstar option does get me recursive
** globbing.  Also, as you found, and unlike ksh, ** globbing
does not match "dot" files&directories.

When I ran the "ls -d **/" under bash with globstar set, it
listed 1413 directories rather than the 8248 ksh reported.
There are a lot of subdirectories under the "dot" directories,
particularly ~/.cache and ~/.config on my system (3500).

Jon
-- 
Jon H. LaBadie                  novalugml@jgcomp.com
 11226 South Shore Rd		(703) 787-0688 (H)
 Reston, VA  20190		(703) 935-6720 (C)



More information about the Novalug mailing list