How do I us ls to show only directories? - Linux

This is a discussion on How do I us ls to show only directories? - Linux ; Using FC3 and this might seem like a stupid question but here goes. I use the command line a lot, especially with ssh from my local XP machine and I want to show only the subdirectories inside of a directory ...

+ Reply to Thread
Results 1 to 18 of 18

Thread: How do I us ls to show only directories?

  1. How do I us ls to show only directories?

    Using FC3 and this might seem like a stupid question but here goes. I use
    the command line a lot, especially with ssh from my local XP machine and I
    want to show only the subdirectories inside of a directory and not every
    file and directory. For some reason, I cannot find the correct string to
    use with ls to only list the directories and not their contents or anything
    like that. I cannot get what I want with:

    ls
    ls -la
    ls -l
    ls -a
    ls -d

    What is the correct way to use the ls command and return only directories?
    I thought that by reading the man page, this would be quite clear as in:

    -d, --directory
    list directory entries instead of contents, and do not dereference
    symbolic links

    This command:
    ls -d
    returns nothing and there are about 5 subdirectories here with a couple of
    hundred files in the public_html directory. I want to show only directories
    and what their permissions are so that I can set the permissions properly
    and would like to use the colors if possible. Doing an ls -la returns so
    much that it is very difficult to hunt out the directory listings. Why does
    not "ls -d" actually work? This is very puzzling, somebody help please.

    --
    ~Ohmster
    "Read Ohmster" in subject, bypass spam filter.
    ohmster /a/t/ newsguy dot com

  2. Re: How do I us ls to show only directories?

    * Ohmster wrote in comp.os.linux:
    > Using FC3 and this might seem like a stupid question but here goes. I use
    > the command line a lot, especially with ssh from my local XP machine and I
    > want to show only the subdirectories inside of a directory and not every
    > file and directory. For some reason, I cannot find the correct string to
    > use with ls to only list the directories and not their contents or anything
    > like that. I cannot get what I want with:


    > ls
    > ls -la
    > ls -l
    > ls -a
    > ls -d


    ls -d * | grep '/'
    ls -p | grep '/'

    Both work for me.

    You can alias them accordingly.
    --
    David
    A chronic disposition to inquiry deprives domestic felines of vital qualities.

  3. Re: How do I us ls to show only directories?

    Ohmster wrote:
    > Using FC3 and this might seem like a stupid question but here goes. I use
    > the command line a lot, especially with ssh from my local XP machine and I
    > want to show only the subdirectories inside of a directory and not every
    > file and directory. For some reason, I cannot find the correct string to
    > use with ls to only list the directories and not their contents or anything
    > like that. I cannot get what I want with:
    >
    > ls
    > ls -la
    > ls -l
    > ls -a
    > ls -d
    >
    > What is the correct way to use the ls command and return only directories?
    > I thought that by reading the man page, this would be quite clear as in:
    >
    > -d, --directory
    > list directory entries instead of contents, and do not dereference
    > symbolic links
    >
    > This command:
    > ls -d
    > returns nothing and there are about 5 subdirectories here with a couple of
    > hundred files in the public_html directory. I want to show only directories
    > and what their permissions are so that I can set the permissions properly
    > and would like to use the colors if possible. Doing an ls -la returns so
    > much that it is very difficult to hunt out the directory listings. Why does
    > not "ls -d" actually work? This is very puzzling, somebody help please.
    >


    ls -ld * | grep '^d'

  4. Re: How do I us ls to show only directories?

    * Ohmster wrote in comp.os.linux:
    > Using FC3 and this might seem like a stupid question but here goes. I use
    > the command line a lot, especially with ssh from my local XP machine and I
    > want to show only the subdirectories inside of a directory and not every
    > file and directory. For some reason, I cannot find the correct string to
    > use with ls to only list the directories and not their contents or anything
    > like that. I cannot get what I want with:


    > ls
    > ls -la
    > ls -l
    > ls -a
    > ls -d


    OK, further investigation makes this easier. After my post I typed
    'alias' at a prompt and noticed

    alias lsd='ls -d */'

    so lsd will give you what you want (LOL
    --
    David
    Riches cover a multitude of woes.
    -- Menander

  5. Re: How do I us ls to show only directories?

    SINNER <99nesorjd@gates_of_hell.invalid> wrote in news:t7ocn2xbe4.ln2
    @news.gates_of_hell.com:

    > ls -d * | grep '/'
    > ls -p | grep '/'
    >
    > Both work for me.
    >
    > You can alias them accordingly.


    First one returns nothing, second one works pretty good. No colors but
    works, pretty good actually. So ls just does not do directories by itself I
    guess. Thanks David.

    --
    ~Ohmster
    "Read Ohmster" in subject, bypass spam filter.
    ohmster /a/t/ newsguy dot com

  6. Re: How do I us ls to show only directories?

    Malte wrote in news:42a3d0d7$0$78282
    $157c6196@dreader1.cybercity.dk:

    > ls -ld * | grep '^d'


    Yeah that works too. Will have to get to work on an alias for this, thanks
    Malte.

    --
    ~Ohmster
    "Read Ohmster" in subject, bypass spam filter.
    ohmster /a/t/ newsguy dot com

  7. Re: How do I us ls to show only directories?

    Ohmster wrote:

    > SINNER <99nesorjd@gates_of_hell.invalid> wrote in news:t7ocn2xbe4.ln2
    > @news.gates_of_hell.com:
    >
    >
    >>ls -d * | grep '/'
    >>ls -p | grep '/'
    >>
    >>Both work for me.
    >>
    >>You can alias them accordingly.
    >>

    >
    > First one returns nothing, second one works pretty good. No colors but
    > works, pretty good actually. So ls just does not do directories by itself I
    > guess. Thanks David.


    Try:

    $ ls -dF * | grep '/$'

    (The F option puts the trailing '/' onto directories. It also adds:
    * -> executable (x bit set)
    = -> named socket
    | -> named pipe
    @ -> symbolic link)


  8. Re: How do I us ls to show only directories?

    In the Usenet newsgroup comp.os.linux, in article
    , Ohmster wrote:

    >I use the command line a lot, especially with ssh from my local XP machine


    He shows promise!

    >and I want to show only the subdirectories inside of a directory and not
    >every file and directory. For some reason, I cannot find the correct
    >string to use with ls to only list the directories and not their contents
    >or anything like that. I cannot get what I want with:


    >ls
    >ls -la
    >ls -l
    >ls -a


    Without an argument (like '*') these command will show all of the
    directories and files in the current working directory. The 'l' gives
    a long listing, and the 'a' shows files whose names begin with a dot.

    >ls -d


    Without an argument, this shows the 'current' directory. WITH an
    argument, like '*' this will show the directory listing BUT not the
    contents of the directories. But it ALSO shows all of the normal files
    in the directory.

    The difference is when you supply an argument like '*'. The basic ls
    command _without_ the -d option would show all files/directories (the
    expansion of the '*' wildcard), AND would show the contents of any
    directories.

    >What is the correct way to use the ls command and return only directories?


    Many ways, depending on how your 'ls' command may be aliased. All ways
    require a pipe to grep to separate the wheat from the chaff. The most
    common usage is

    ls -ld * | grep '^d'
    or
    ls -F | grep '/'

    or even just

    ls -ld */

    >This command:
    >ls -d
    >returns nothing and there are about 5 subdirectories here with a couple of
    >hundred files in the public_html directory.


    Depends on what your command is aliased to - try the command 'alias' and
    see what it returns. In my case, 'ls -d' returns a dot, which is to say
    the current directory. Pretty useless.

    >I want to show only directories and what their permissions are so that I
    >can set the permissions properly and would like to use the colors if
    >possible.


    I don't use colors (often I'm working over the net, and many terminals don't
    support colors), but this specific problem wants "ls -ld */' which is to
    say "show me the directories (*/) but not what's in them (-d)".

    >Doing an ls -la returns so much that it is very difficult to hunt out the
    >directory listings.


    Yeah, some of the directories I work with have over 10,000 files in them.
    You learn to be more selective with grep, real quick.

    >Why does not "ls -d" actually work? This is very puzzling, somebody help
    >please.


    Well, it does work - it's just not working the way you'd expect it to. The
    reason is your interpretation of the man page - it gives the answer in
    English, but you are reading it in English. See the problem? ;-)

    The best solution is something like training wheels - you start playing
    with the command in a smaller directory where you can see what the command
    is doing.

    [spitzer ~] ls -l *
    -rw-r--r-- 1 ibuprofi users 1953 Jul 17 2004 duty.hours

    bin:
    total 1
    -rwx------ 1 ibuprofi users 200 Jul 17 2004 online.news

    mail:
    total 4
    -rw------- 1 ibuprofi users 506 Jul 15 2004 saved-messages
    -rw------- 1 ibuprofi users 1187 May 17 13:47 sent-mail
    [spitzer ~]

    Notice how this shows "everything" including the _contents_ of the
    directories.

    [spitzer ~] ls -ld *
    drwxr-xr-x 2 ibuprofi users 1024 Jul 16 2004 bin
    -rw-r--r-- 1 ibuprofi users 1953 Jul 17 2004 duty.hours
    drwxr-xr-x 2 ibuprofi users 1024 May 17 13:47 mail
    [spitzer ~] ls -ld * | grep '^d'
    drwxr-xr-x 2 ibuprofi users 1024 Jul 16 2004 bin
    drwxr-xr-x 2 ibuprofi users 1024 May 17 13:47 mail
    [spitzer ~] ls -ld */
    drwxr-xr-x 2 ibuprofi users 1024 Jul 16 2004 bin
    drwxr-xr-x 2 ibuprofi users 1024 May 17 13:47 mail
    [spitzer ~]

    So, find a small enough directory setup, and go play!

    Old guy

  9. Re: How do I us ls to show only directories?

    ibuprofin@painkiller.example.tld (Moe Trin) wrote in
    news:slrnda9qo2.1cr.ibuprofin@compton.phx.az.us:

    > or even just
    >
    > ls -ld */


    Oh this is exactly what I want, and it gives the colors! ...well, the
    results are in blue and since all directories are in blue, this is not
    very informative so perhaps it is not such a big deal now. The colors
    help with picking directories out of a listing, they are all deep blue.

    Hmmm, "alias" does show what is going on for the system:

    [ohmster@ohmster ~]$ alias
    alias l.='ls -d .* --color=tty'
    alias ll='ls -l --color=tty'
    alias ls='ls --color=tty'
    alias mc='. /usr/share/mc/bin/mc-wrapper.sh'
    alias vi='vim'
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --
    show-tilde'
    alias www='cd $HOME/public_html'
    [ohmster@ohmster ~]$

    The first one:
    l.
    ....is close to what I want but not something I would use much, I am going
    to "take over that alias" and turn it into your suggestion:
    ls -ld */

    (Oh I had better not do that, I will make my own alias rather than "break
    something" that might be useful later on.)

    Oh freaking kewl!!! I just copied the alias reported from the system into
    the /etc/profile file and modified it a bit as such:

    alias l..='ls -d */ --color=tty'

    Now when I want to see just directories, I do:
    l..

    And there are only the directories, just like I wanted! This rocks man.
    Funny thing, I looked at where those alias lines might be and could not
    find them in my ~/.bashrc file, /etc/profile, or /etc/bashrc. I wonder
    where those alias lines are on my system, anyway? The www one to put me
    in my ~/public_html directory I did myself in my own ~/.bashrc file, the
    rest, who knows where they came from?

    Old Guy, your explanation was most *excellent* and I thank you for it, I
    will read it over again and learn from it, this one post I did save in my
    "linux tidbit" directory, where I save all of the really good stuff that
    I get from Usenet.

    I was telling the wife about how "Old Guy" often saves the day with
    really good help and was telling her that your real name is ""Moe Trin"
    until she looked at me and said "Motrin, yeah, uh huh, take an Advil with
    that".

    ....sigh.

    Thanks for your very educational and informative reply.

    I liked that alias so much that I added another one:
    alias l...='ls -ld */ --color=tty'
    ....too cool man. (Yeah I checked that l.. and l... did not actually do
    anything before I assigned aliases to them.)

    Yeah great stuff, Old Guy, going back to read your post and experiment. I
    did manage to get my sudo thing worked out to my satisfaction. Thanks on
    that too.

    --
    ~Ohmster
    "Read Ohmster" in subject, bypass spam filter.
    ohmster /a/t/ newsguy dot com

  10. Re: How do I us ls to show only directories?

    SINNER <99nesorjd@gates_of_hell.invalid> wrote in news:83tdn2xdnb.ln2
    @news.gates_of_hell.com:

    > alias lsd='ls -d */'
    >
    > so lsd will give you what you want (LOL
    > --


    Ahhh!

    Yeah I added that now, good aliases I got now. Look see:

    [ohmster@ohmster public_html]$ alias
    alias l.='ls -d .* --color=tty'
    alias l..='ls -d */ --color=tty'
    alias l...='ls -ld */ --color=tty'
    alias ll='ls -l --color=tty'
    alias ls='ls --color=tty'
    alias mc='. /usr/share/mc/bin/mc-wrapper.sh'
    alias vi='vim'
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --
    show-tilde'
    alias www='cd $HOME/public_html'
    [ohmster@ohmster public_html]$

    Thanks for your time, Dave.

    --
    ~Ohmster
    "Read Ohmster" in subject, bypass spam filter.
    ohmster /a/t/ newsguy dot com

  11. Re: How do I us ls to show only directories?

    Robert Newson wrote in news:42A49C2B.20207
    @bullet3.fsnet.oc.ku:

    > Try:
    >
    > $ ls -dF * | grep '/$'
    >
    > (The F option puts the trailing '/' onto directories. It also adds:
    > * -> executable (x bit set)
    > = -> named socket
    > | -> named pipe
    > @ -> symbolic link)


    Very informative, but I like the colors better (Yours does work though.). I
    wrote you back in your next post. Thanks.

    --
    ~Ohmster
    "Read Ohmster" in subject, bypass spam filter.
    ohmster /a/t/ newsguy dot com

  12. Re: How do I us ls to show only directories?

    ibuprofin@painkiller.example.tld (Moe Trin) wrote in
    news:slrnda9qo2.1cr.ibuprofin@compton.phx.az.us:

    > In the Usenet newsgroup comp.os.linux, in article
    > , Ohmster wrote:
    >
    >>I use the command line a lot, especially with ssh from my local XP
    >>machine

    >
    > He shows promise!


    Thank you, thank you very much.

    >
    > Depends on what your command is aliased to - try the command 'alias'
    > and see what it returns. In my case, 'ls -d' returns a dot, which is
    > to say the current directory. Pretty useless.


    No doubt, pretty useless.

    >
    > I don't use colors (often I'm working over the net, and many terminals
    > don't support colors), but this specific problem wants "ls -ld */'
    > which is to say "show me the directories (*/) but not what's in them
    > (-d)".


    I use SecureCRT mostly and work on a terminal from my XP machine to the
    linux box right next to me. I have a better monitor on the XP machine,
    21", so I like that a little more than the 17" one on the linux box.
    SecureCRT is nice in that it will do ANSI colors in a VT100 terminal, so
    I do get to see the colors in my case. The colors are neat in that you
    can easily spot certain kinds of files with them. I added iso to the list
    of compressed files in /etc/DIR_COLORS so that they show up red. I wish
    there were more colors available, I want more colors for media files like
    audio and video.

    I put audio and video files as cyan but that is the same as links so I
    will probably take that color away from the audio files now. Links are
    more important to spot than multimedia files.

    > Yeah, some of the directories I work with have over 10,000 files in
    > them. You learn to be more selective with grep, real quick.


    Whoa, I see your point, man.

    > Well, it does work - it's just not working the way you'd expect it to.
    > The reason is your interpretation of the man page - it gives the
    > answer in English, but you are reading it in English. See the
    > problem? ;-)


    Yes yes, I see now.


    >
    > So, find a small enough directory setup, and go play!
    >
    > Old guy
    >


    Good tip, thanks. Off to go play. See ya.

    --
    ~Ohmster
    "Read Ohmster" in subject, bypass spam filter.
    ohmster /a/t/ newsguy dot com

  13. Re: How do I us ls to show only directories?

    On 2005-06-06, Ohmster wrote:
    >
    > What is the correct way to use the ls command and return only directories?
    > I thought that by reading the man page, this would be quite clear as in:


    This has been answered many times in several newsgroups:

    ls -d */

    You have to remember that ls does not expand the arguments; that
    is done by the shell.

    --
    Chris F.A. Johnson
    ================================================== ================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress


  14. Re: How do I us ls to show only directories?

    * Ohmster wrote in comp.os.linux:
    > SINNER <99nesorjd@gates_of_hell.invalid> wrote in news:83tdn2xdnb.ln2
    > @news.gates_of_hell.com:


    >> alias lsd='ls -d */'


    >> so lsd will give you what you want (LOL


    > Ahhh!


    > Yeah I added that now, good aliases I got now. Look see:


    > [ohmster@ohmster public_html]$ alias
    > alias l.='ls -d .* --color=tty'
    > alias l..='ls -d */ --color=tty'
    > alias l...='ls -ld */ --color=tty'
    > alias ll='ls -l --color=tty'
    > alias ls='ls --color=tty'
    > alias mc='. /usr/share/mc/bin/mc-wrapper.sh'
    > alias vi='vim'
    > alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --
    > show-tilde'
    > alias www='cd $HOME/public_html'
    > [ohmster@ohmster public_html]$


    > Thanks for your time, Dave.


    No problem, I learned something too

    --
    David
    If you have to hate, hate gently.

  15. Re: How do I us ls to show only directories?

    In the Usenet newsgroup comp.os.linux, in article
    , Ohmster wrote:


    >I was telling the wife about how "Old Guy" often saves the day with
    >really good help and was telling her that your real name is ""Moe Trin"
    >until she looked at me and said "Motrin, yeah, uh huh, take an Advil with
    >that".


    Nah, Advil is my evil twin brother.

    >...sigh.


    I don't know why everyone gives me so much grief over that - if you look
    at the "mail" name I'm using, it should be _slightly_ obvious. ;-)

    >Thanks for your very educational and informative reply.


    Pleasure!

    Old guy


  16. Re: How do I us ls to show only directories?

    "Chris F.A. Johnson" wrote in news:vrrfn2-0sk.ln1
    @rogers.com:

    > This has been answered many times in several newsgroups:


    No doubt it has. Havn't found the answer that I needed though until asking
    here.

    > ls -d */
    >
    > You have to remember that ls does not expand the arguments; that
    > is done by the shell.


    Learning about it now and I have a working alias that will do what I need,
    this is great. Thanks everybody.

    alias l.='ls -d .* --color=tty'
    alias l..='ls -d */ --color=tty'
    alias l...='ls -ld */ --color=tty'
    alias ll='ls -l --color=tty'
    alias ls='ls --color=tty'

    I like yours Chris, but end up using the
    ls -ld */
    variation more as it gives me the permissions and that is what is more
    useful for html work. Thanks for your answer.

    --
    ~Ohmster
    "Read Ohmster" in subject, bypass spam filter.
    ohmster /a/t/ newsguy dot com

  17. Re: How do I us ls to show only directories?

    ibuprofin@painkiller.example.tld (Moe Trin) wrote in
    news:slrndabv5r.66r.ibuprofin@compton.phx.az.us:

    >>I was telling the wife about how "Old Guy" often saves the day with
    >>really good help and was telling her that your real name is ""Moe Trin"
    >>until she looked at me and said "Motrin, yeah, uh huh, take an Advil with
    >>that".

    >
    > Nah, Advil is my evil twin brother.


    Heh.

    >>...sigh.

    >
    > I don't know why everyone gives me so much grief over that - if you look
    > at the "mail" name I'm using, it should be _slightly_ obvious. ;-)


    From: ibuprofin@painkiller.example.tld (Moe Trin)
    Reply-To: no.mail.accepted.sorry

    Don't mean to give you grief buddy, just happened to notice it and
    commented, that is all.

    >>Thanks for your very educational and informative reply.

    >
    > Pleasure!


    Same here for sure.

    > Old guy
    >


    Take care Old Guy!

    --
    ~Ohmster
    "Read Ohmster" in subject, bypass spam filter.
    ohmster /a/t/ newsguy dot com

  18. Re: How do I us ls to show only directories?

    In the Usenet newsgroup comp.os.linux, in article
    , Ohmster wrote:

    >ibuprofin@painkiller.example.tld (Moe Trin) wrote in
    >news:slrndabv5r.66r.ibuprofin@compton.phx.az.us:


    >> Nah, Advil is my evil twin brother.

    >
    >Heh.


    I'd introduce you to my sister Vi Oxx, ,
    but she's been rather withdrawn lately.

    >> I don't know why everyone gives me so much grief over that - if you look
    >> at the "mail" name I'm using, it should be _slightly_ obvious. ;-)

    >
    >From: ibuprofin@painkiller.example.tld (Moe Trin)
    >Reply-To: no.mail.accepted.sorry
    >
    >Don't mean to give you grief buddy, just happened to notice it and
    >commented, that is all.


    Sheesh - now I'm probably gonna be audited by the FDA.

    >Take care Old Guy!


    Is that a new medication for paranoia? ;-)

    Old guy

+ Reply to Thread