PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > list folders only!
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

list folders only!

Réponse
 
LinkBack Outils de la discussion
Vieux 24/03/2008, 11h24   #1
Marek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut list folders only!



Hello all!



Hope this is not a faq. My question seems easy, but the only solution
I found is a bit complicate. To list folders only, I have had first
the idea

% ls | grep ^d

which is not working here! I am on Macintosh OS 10.4.11 and my shell
is tcsh. Also quoting did not :

% ls | grep "^d"

Why? Ok! Then I tried these following perl solutions, which is a bit
complicate:

% ls -s | perl -e 'while (<>) {print if (/^\s*0/)}'

and this is working too:

% ls -l | perl -e 'while (<>) {print if (/^\s*d/)}'

is there a more elegant solution to this problem?



happy Eastern to all



marek
  Réponse avec citation
Vieux 24/03/2008, 11h37   #2
Salve Håkedal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!

On 2008-03-24, Marek <mstep@podiuminternational.org> wrote:
>
>
> Hello all!
>
>
>
> Hope this is not a faq. My question seems easy, but the only solution
> I found is a bit complicate. To list folders only, I have had first
> the idea
>
> % ls | grep ^d
>
> which is not working here! I am on Macintosh OS 10.4.11 and my shell
> is tcsh. Also quoting did not :
>
> % ls | grep "^d"
>
> Why? Ok! Then I tried these following perl solutions, which is a bit
> complicate:
>
> % ls -s | perl -e 'while (<>) {print if (/^\s*0/)}'
>
> and this is working too:
>
> % ls -l | perl -e 'while (<>) {print if (/^\s*d/)}'
>
> is there a more elegant solution to this problem?
>

In bash:
$ ls -d */
or as I prefer:
$ ls -1d */

--
Salve
  Réponse avec citation
Vieux 24/03/2008, 12h04   #3
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!



Salve Håkedal wrote:
> On 2008-03-24, Marek <mstep@podiuminternational.org> wrote:
>>
>> I found is a bit complicate. To list folders only
>> [...]
>> is there a more elegant solution to this problem?
>>

> In bash:
> $ ls -d */
> or as I prefer:
> $ ls -1d */


or

$ ls -1d .*/ */

--
Best regards | Monica Lewinsky's X-Boyfriend's
Cyrus | Wife for President
  Réponse avec citation
Vieux 24/03/2008, 12h07   #4
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!

Cyrus Kriticos wrote:
>
> Salve Håkedal wrote:
>> On 2008-03-24, Marek <mstep@podiuminternational.org> wrote:
>>>
>>> I found is a bit complicate. To list folders only
>>> [...]
>>> is there a more elegant solution to this problem?
>>>

>> In bash:
>> $ ls -d */
>> or as I prefer:
>> $ ls -1d */

>
> or
>
> $ ls -1d .*/ */


or

$ find . -type d -maxdepth 1 -ls

--
Best regards | Monica Lewinsky's X-Boyfriend's
Cyrus | Wife for President
  Réponse avec citation
Vieux 24/03/2008, 12h07   #5
Marek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!

On Mar 24, 11:37 am, Salve Håkedal <ikkjespam.sa...@slogedalen.no>
wrote:

>
> In bash:
> $ ls -d */
> or as I prefer:
> $ ls -1d */
>
> --
> Salve


Wow! this was a quick answer :-) And it is working too in my tcsh! And
now I realize, why my suggestion of

% ls | grep ^d

was not working!

It needs the switch of -l of course!


% ls -l | grep ^d

Sorry for the noise! And thanks to Salve


marek
  Réponse avec citation
Vieux 24/03/2008, 12h08   #6
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!

Cyrus Kriticos wrote:
>
> Salve Håkedal wrote:
>> On 2008-03-24, Marek <mstep@podiuminternational.org> wrote:
>>>
>>> I found is a bit complicate. To list folders only
>>> [...]
>>> is there a more elegant solution to this problem?
>>>

>> In bash:
>> $ ls -d */
>> or as I prefer:
>> $ ls -1d */

>
> or
>
> $ ls -1d .*/ */


or with GNU find

$ find . -type d -maxdepth 1 -ls

--
Best regards | Monica Lewinsky's X-Boyfriend's
Cyrus | Wife for President
  Réponse avec citation
Vieux 24/03/2008, 12h12   #7
Maxwell Lol
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!

Marek <mstep@podiuminternational.org> writes:

> % ls | grep ^d


Here's a couple of aliases

alias lsd '/bin/ls -aF | grep /$ | xargs /bin/ls -Cd'
alias ldir '/bin/ls -l|/bin/grep "^d"'
alias dir 'set d = `ls -1F \!* |grep /`; if ($#d) echo "$d:gh";unset d'
  Réponse avec citation
Vieux 24/03/2008, 12h59   #8
John W. Krahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!

Marek wrote:
>
> Hope this is not a faq. My question seems easy, but the only solution
> I found is a bit complicate. To list folders only, I have had first
> the idea
>
> % ls | grep ^d
>
> which is not working here! I am on Macintosh OS 10.4.11 and my shell
> is tcsh. Also quoting did not :
>
> % ls | grep "^d"
>
> Why? Ok! Then I tried these following perl solutions, which is a bit
> complicate:
>
> % ls -s | perl -e 'while (<>) {print if (/^\s*0/)}'
>
> and this is working too:
>
> % ls -l | perl -e 'while (<>) {print if (/^\s*d/)}'
>
> is there a more elegant solution to this problem?


perl -le'-d&&print for<*>'



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
  Réponse avec citation
Vieux 24/03/2008, 14h16   #9
Joel Reicher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!

Marek <mstep@podiuminternational.org> writes:

> Hope this is not a faq. My question seems easy, but the only solution
> I found is a bit complicate. To list folders only, I have had first
> the idea
>
> % ls | grep ^d


You have some good answers to your question already, but you might get
an even better answer if you say what you want a list of folders
for. I often find that a shell command is awkward not because of some
deficiency in the UNIX environment, but because the user requirement
is poorly understood.

So what do you need this folder list for?

Cheers,

- Joel
  Réponse avec citation
Vieux 24/03/2008, 14h50   #10
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!

2008-03-24, 05:37(-05), Salve Håkedal:
[...]
> In bash:
> $ ls -d */


should be ls -d -- */

> or as I prefer:
> $ ls -1d */


It works in most shells, not only bash. But note that it lists
directories and symlinks to directories, it also adds a / to the
end of every file name.

zsh has globbing qualifiers to select files of certain types or
with certain attributes:

ls -ld -- *(/)

will select directories only.

ls -ld -- *(-/)

for directories or symlinks to directories.

ls -ld -- *(D-/)

also includes dot-dirs (hidden directories).

ls -ld -- *(oND-/)

to disable the sorting done by zsh (ls does the sorting already
anyway)

With find:

find . ! -name . -prune ! -name '.*' -type d -print

Or:

find . ! -name . -prune ! -name '.*' -follow -type d -print
(to include symlinks to directories)

or:

find . ! -name . -prune -follow -type d -print
(to include hidden dirs).


--
Stéphane
  Réponse avec citation
Vieux 24/03/2008, 17h28   #11
Marek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!

On Mar 24, 2:50 pm, Stephane CHAZELAS <this.addr...@is.invalid> wrote:

>
> --
> Stéphane


Merci Stéphane, très rafraîchissant tes suggestions :-) Pleins des
choses à apprendre! -prune je connaissais pas encore avec "find".


Thank you all, for your answers ... Have to read many man-pages
again :-)


Greetings from Munich


marek
  Réponse avec citation
Vieux 24/03/2008, 21h12   #12
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!

Maxwell Lol wrote:
> Marek <mstep@podiuminternational.org> writes:
>
>> % ls | grep ^d

>
> Here's a couple of aliases
>
> alias lsd '/bin/ls -aF | grep /$ | xargs /bin/ls -Cd'
> alias ldir '/bin/ls -l|/bin/grep "^d"'
> alias dir 'set d = `ls -1F \!* |grep /`; if ($#d) echo "$d:gh";unset d'


"if" without "then" and "fi"?

--
Best regards | Monica Lewinsky's X-Boyfriend's
Cyrus | Wife for President
  Réponse avec citation
Vieux 24/03/2008, 21h23   #13
Maxwell Lol
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!

pk <pk@pk.invalid> writes:

> Cyrus Kriticos wrote:
>
> >> alias dir 'set d = `ls -1F \!* |grep /`; if ($#d) echo "$d:gh";unset d'

> >
> > "if" without "then" and "fi"?

>
> I think that's tcsh syntax.


Which is what the OP mentioned....
  Réponse avec citation
Vieux 24/03/2008, 21h26   #14
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!



Maxwell Lol wrote:
> pk <pk@pk.invalid> writes:
>
>> Cyrus Kriticos wrote:
>>
>>>> alias dir 'set d = `ls -1F \!* |grep /`; if ($#d) echo "$d:gh";unset d'
>>> "if" without "then" and "fi"?

>> I think that's tcsh syntax.

>
> Which is what the OP mentioned....


thanks for this hint

--
Best regards | Monica Lewinsky's X-Boyfriend's
Cyrus | Wife for President
  Réponse avec citation
Vieux 24/03/2008, 21h36   #15
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: list folders only!

Cyrus Kriticos wrote:

>> alias dir 'set d = `ls -1F \!* |grep /`; if ($#d) echo "$d:gh";unset d'

>
> "if" without "then" and "fi"?


I think that's tcsh syntax.

tcsh> if (10 == 10) echo "ok"
ok

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h34.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19743 seconds with 23 queries