ls just directories

ls just directories

am 04.10.2007 12:21:23 von SiKing

Dear all,

I have the following fictional data:

$ ls -lAF someplace/
-rw------- 1 cpuser cpgroup 48 Oct 3 10:10 .hidden_file
-rw------- 1 cpuser cpgroup 48 Oct 3 10:10 normal_file
drwx------ 1 cpuser cpgroup 48 Oct 3 10:10 .hidden_directory/
drwx------ 1 cpuser cpgroup 48 Oct 3 10:10 normal_directory_1/
drwx------ 1 cpuser cpgroup 48 Oct 3 10:10 normal_directory_2/

I need to process, in bash, just the non-hidden directories in a loop. In my
example above, only entries "normal_directory_1" and "normal_directory_2" should
be processed as plain strings. Right now I do it like this:

shopt -u dotglob # ignore dot (hidden) files
for _dirs in $(find someplace/* -maxdepth 1 -type d -print)
do
# Massage the pathname.
_dirs=$(basename $_dirs)

# insert processing of $_dirs here!
done

Is there a better way, preferably one that does not rely on find?

Thank You.

Re: ls just directories

am 04.10.2007 12:37:38 von cichomitiko

SiKing wrote:
> Dear all,
>
> I have the following fictional data:
>
> $ ls -lAF someplace/
> -rw------- 1 cpuser cpgroup 48 Oct 3 10:10 .hidden_file
> -rw------- 1 cpuser cpgroup 48 Oct 3 10:10 normal_file
> drwx------ 1 cpuser cpgroup 48 Oct 3 10:10 .hidden_directory/
> drwx------ 1 cpuser cpgroup 48 Oct 3 10:10 normal_directory_1/
> drwx------ 1 cpuser cpgroup 48 Oct 3 10:10 normal_directory_2/
>
> I need to process, in bash, just the non-hidden directories in a loop.
> In my example above, only entries "normal_directory_1" and
> "normal_directory_2" should be processed as plain strings. Right now I
> do it like this:
>
> shopt -u dotglob # ignore dot (hidden) files
> for _dirs in $(find someplace/* -maxdepth 1 -type d -print)
> do
> # Massage the pathname.
> _dirs=$(basename $_dirs)
>
> # insert processing of $_dirs here!
> done
>
> Is there a better way, preferably one that does not rely on find?

[...]

for _dirs in */;do ...



Dimitre

Re: ls just directories

am 04.10.2007 13:24:13 von cichomitiko

Radoulov, Dimitre wrote:
[...]
>>
>> I need to process, in bash, just the non-hidden directories in a loop.
>> In my example above, only entries "normal_directory_1" and
>> "normal_directory_2" should be processed as plain strings. Right now I
>> do it like this:
>>
>> shopt -u dotglob # ignore dot (hidden) files
>> for _dirs in $(find someplace/* -maxdepth 1 -type d -print)
[...]
>> Is there a better way, preferably one that does not rely on find?
>
> [...]
>
> for _dirs in */;do ...

In your case it will be:

for _dirs in someplace/*/;do ...

,of course :)


Dimitre

Re: ls just directories

am 04.10.2007 15:07:24 von SiKing

Radoulov, Dimitre wrote:
> Radoulov, Dimitre wrote:
> [...]
>>>
>>> I need to process, in bash, just the non-hidden directories in a
>>> loop. In my example above, only entries "normal_directory_1" and
>>> "normal_directory_2" should be processed as plain strings. Right now
>>> I do it like this:
>>>
>>> shopt -u dotglob # ignore dot (hidden) files
>>> for _dirs in $(find someplace/* -maxdepth 1 -type d -print)
> [...]
>>> Is there a better way, preferably one that does not rely on find?
>>
>> [...]
>>
>> for _dirs in */;do ...
>
> In your case it will be:
>
> for _dirs in someplace/*/;do ...
>
> ,of course :)
>
>
> Dimitre

First thing I started looking at was some variant of 'ls -d', which of course
did not do it. Then I resorted to that thing above. Could not sleep all night
over _that_ solution. :)) Surely enough, then next day; Solaris-find does not
understand -maxdepth!
Thank you for pointing the obvious! :)

Re: ls just directories

am 04.10.2007 15:15:07 von Jobs Circular group

On Oct 4, 8:07 am, SiKing wrote:
> Radoulov, Dimitre wrote:
> > Radoulov, Dimitre wrote:
> > [...]
>
> >>> I need to process, in bash, just the non-hidden directories in a
> >>> loop. In my example above, only entries "normal_directory_1" and
> >>> "normal_directory_2" should be processed as plain strings. Right now
> >>> I do it like this:
>
> >>> shopt -u dotglob # ignore dot (hidden) files
> >>> for _dirs in $(find someplace/* -maxdepth 1 -type d -print)
> > [...]
> >>> Is there a better way, preferably one that does not rely on find?
>
> >> [...]
>
> >> for _dirs in */;do ...
>
> > In your case it will be:
>
> > for _dirs in someplace/*/;do ...
>
> > ,of course :)
>
> > Dimitre
>
> First thing I started looking at was some variant of 'ls -d', which of course
> did not do it. Then I resorted to that thing above. Could not sleep all night
> over _that_ solution. :)) Surely enough, then next day; Solaris-find does not
> understand -maxdepth!
> Thank you for pointing the obvious! :)- Hide quoted text -
>
> - Show quoted text -

try

ls -l | grep drw

Re: ls just directories

am 04.10.2007 15:33:08 von Stephane CHAZELAS

2007-10-04, 14:07(+01), SiKing:
[...]
> First thing I started looking at was some variant of 'ls -d', which of course
> did not do it. Then I resorted to that thing above. Could not sleep all night
> over _that_ solution. :)) Surely enough, then next day; Solaris-find does not
> understand -maxdepth!
[...]

-maxdepth is a GNU extension also found in FreeBSD find (at
least).

The standard equivalent it:

find dir/. \! -name . -prune -type d -exec \
whatever you want with dir {} \;

> Thank you for pointing the obvious! :)

Not that obvious (obviously standard at least).

It's clearer that

for dir in dir/*/.; do ...

is standard.

--
Stéphane

Re: ls just directories

am 04.10.2007 21:03:09 von Michael Tosch

SiKing wrote:
> Radoulov, Dimitre wrote:
>> Radoulov, Dimitre wrote:
>> [...]
>>>>
>>>> I need to process, in bash, just the non-hidden directories in a
>>>> loop. In my example above, only entries "normal_directory_1" and
>>>> "normal_directory_2" should be processed as plain strings. Right now
>>>> I do it like this:
>>>>
>>>> shopt -u dotglob # ignore dot (hidden) files
>>>> for _dirs in $(find someplace/* -maxdepth 1 -type d -print)
>> [...]
>>>> Is there a better way, preferably one that does not rely on find?
>>>
>>> [...]
>>>
>>> for _dirs in */;do ...
>>
>> In your case it will be:
>>
>> for _dirs in someplace/*/;do ...
>>
>> ,of course :)
>>
>>
>> Dimitre
>
> First thing I started looking at was some variant of 'ls -d', which of
> course did not do it. Then I resorted to that thing above. Could not
> sleep all night over _that_ solution. :)) Surely enough, then next day;
> Solaris-find does not understand -maxdepth!
> Thank you for pointing the obvious! :)

With Solaris find it would be something ugly like

dir=someplace
find "$dir" \! -name "$dir" -type d -prune \! -name ".*" -print


--
Michael Tosch @ hp : com

Re: ls just directories

am 05.10.2007 17:03:30 von Brian Mac

hi siking,

how about

for entry in $(ls parent_dir)
do
if [ -d $entry ]; then
do_something
fi
done

hope this helps,
brian

On Oct 4, 3:03 pm, Michael Tosch
wrote:
> SiKing wrote:
> > Radoulov, Dimitre wrote:
> >> Radoulov, Dimitre wrote:
> >> [...]
>
> >>>> I need to process, in bash, just the non-hidden directories in a
> >>>> loop. In my example above, only entries "normal_directory_1" and
> >>>> "normal_directory_2" should be processed as plain strings. Right now
> >>>> I do it like this:
>
> >>>> shopt -u dotglob # ignore dot (hidden) files
> >>>> for _dirs in $(find someplace/* -maxdepth 1 -type d -print)
> >> [...]
> >>>> Is there a better way, preferably one that does not rely on find?
>
> >>> [...]
>
> >>> for _dirs in */;do ...
>
> >> In your case it will be:
>
> >> for _dirs in someplace/*/;do ...
>
> >> ,of course :)
>
> >> Dimitre
>
> > First thing I started looking at was some variant of 'ls -d', which of
> > course did not do it. Then I resorted to that thing above. Could not
> > sleep all night over _that_ solution. :)) Surely enough, then next day;
> > Solaris-find does not understand -maxdepth!
> > Thank you for pointing the obvious! :)
>
> With Solaris find it would be something ugly like
>
> dir=someplace
> find "$dir" \! -name "$dir" -type d -prune \! -name ".*" -print
>
> --
> Michael Tosch @ hp : com- Hide quoted text -
>
> - Show quoted text -

Re: ls just directories

am 05.10.2007 19:54:34 von cfajohnson

On 2007-10-05, Brian Mac wrote:

[please don't top post]

> how about
>
> for entry in $(ls parent_dir)

Not only is ls unnecessary, but it will break the script if there
are spaces or other pathological characters in a filename. Use:

for entry in parent_dir/*

> do
> if [ -d $entry ]; then
> do_something
> fi
> done


--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence