Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Listing directories only without slash

37 views
Skip to first unread message

James

unread,
Apr 5, 2017, 5:33:15 PM4/5/17
to
Suppose I have
dirs: aa bb cc
files: ff gg

$ ls -d *
aa bb cc ff gg

$ ls -d */
aa/ bb/ cc/

How do I list directories only without slash at the end?

TIA
James

connor

unread,
Apr 5, 2017, 5:47:00 PM4/5/17
to
On Wed, 5 Apr 2017 14:33:04 -0700 (PDT), James <hsle...@yahoo.com> wrote:

I'm sure there are better ways but you could use tr to delete the slash:

$ ls -d */ | tr -d \/

You could also explore the use of find:

$ find . -type d -print

Cheers,
Gary

Lew Pitcher

unread,
Apr 5, 2017, 5:50:11 PM4/5/17
to
connor wrote:

> On Wed, 5 Apr 2017 14:33:04 -0700 (PDT), James <hsle...@yahoo.com> wrote:
>
> I'm sure there are better ways but you could use tr to delete the slash:
>
> $ ls -d */ | tr -d \/
>
> You could also explore the use of find:
>
> $ find . -type d -print
Try
find . -maxdepth 1 -type d -print
to limit yourself to only those directories in CWD (that is, /not/ list
subdirectories of directories in CWD)

> Cheers,
> Gary
>>Suppose I have
>>dirs: aa bb cc
>>files: ff gg
>>
>>$ ls -d *
>>aa bb cc ff gg
>>
>>$ ls -d */
>>aa/ bb/ cc/
>>
>>How do I list directories only without slash at the end?
>>
>>TIA
>>James


--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request


Helmut Waitzmann

unread,
Apr 5, 2017, 6:58:37 PM4/5/17
to
James <hsle...@yahoo.com>:
Using the shell:

set '' */ && shift &&
for d
do
set '' "$@" "${d%/}" && shift 2
done &&
ls -d -- "$@"

Helmut Waitzmann

unread,
Apr 5, 2017, 7:15:13 PM4/5/17
to
Lew Pitcher <lew.p...@digitalfreehold.ca>:
> connor wrote:
>
>> On Wed, 5 Apr 2017 14:33:04 -0700 (PDT), James <hsle...@yahoo.com> wrote:
>>
>> I'm sure there are better ways but you could use tr to delete the slash:
>>
>> $ ls -d */ | tr -d \/
>>
>> You could also explore the use of find:
>>
>> $ find . -type d -print
> Try
> find . -maxdepth 1 -type d -print
> to limit yourself to only those directories in CWD (that is, /not/ list
> subdirectories of directories in CWD)

Or, if your find does not understand -maxdepth (which is not
defined by POSIX as far as I know), you might use -prune instead:

find -L -- * ! -type d -o \( -prune -exec ls -d -- '{}' + \)

Janis Papanagnou

unread,
Apr 5, 2017, 7:29:41 PM4/5/17
to
Another shell variant (tested with ksh, bash, zsh)...

set -- */ ; printf "%s\n" "${@%/}"


Janis

>
> TIA
> James
>

Janis Papanagnou

unread,
Apr 5, 2017, 7:54:55 PM4/5/17
to
Or maybe

set -- */ ; [[ -d "$1" ]] && printf "%s\n" "${@%/}"

(to not show a '*' in case of no directories to display).

>
> Janis
>
>>
>> TIA
>> James
>>
>

Rakesh Sharma

unread,
Apr 6, 2017, 2:57:54 AM4/6/17
to
find . -type d ! -name . -prune

will print all the directories in the current dir.

Helmut Waitzmann

unread,
Apr 6, 2017, 5:11:18 AM4/6/17
to
Rakesh Sharma <shar...@hotmail.com>:
> On Thursday, 6 April 2017 04:45:13 UTC+5:30, Helmut Waitzmann wrote:

> find . -type d ! -name . -prune
>
> will print all the directories in the current dir.

Correct.

But if I understand the OP correctly, it is not exactly, what James
wanted. He asked for the output of

$ ls -d -- */

but with the trailing slashes removed. Admittedly, his example is
not complete: It contains neither a directory name (other than
“.” or “..”) starting with a dot nor a symbolic link referring to
a directory.

$ PS2='? '
$ mkdir -- test && cd test &&
? mkdir -- aa bb cc .dd &&
? ln -s -- . ee &&
? touch -- ff gg &&
? ls -d -- */
aa/ bb/ cc/ ee/

$ find . -type d ! -name . -prune
./bb
./.dd
./aa
./cc

$ find -L -- * ! -type d -o \( -prune -exec ls -d -- '{}' + \)
aa bb cc ee

Rakesh Sharma

unread,
Apr 6, 2017, 10:31:26 AM4/6/17
to
You are very right. But I was trying to point out that the '-o' can be dispensed with...

find -L -- * -type d -prune -exec ls -d -- '{}' +

Helmut Waitzmann

unread,
Apr 6, 2017, 7:21:54 PM4/6/17
to
Thank you for the clarification. I think, both forms are
equivalent with respect to the question, which filenames will be
supplied to the invocation of “ls”.
0 new messages