Display directories only

Solved
artagon7 Posted messages 423 Status Member -  
 dennys -
Hello,

I want to display only the directories contained in a given directory. I read the man page for ls and what I found is the -d option, but it doesn't work. How do we display only the directories (recursively)?

Thank you
Configuration: Linux Konqueror 3.5

9 answers

  1. medem Posted messages 2 Status Member 9
     
    Hello!
    You can also use "ls -d */"
    ls -d: to tell it not to descend into subdirectories
    */: to tell it to display all those that end with "/" which are only the directories
    +
    12
    1. artagon7 Posted messages 423 Status Member 7
       
      Noted. Thank you.
      0
    2. dennys
       
      Thank you. Useful and complete.
      0
  2. marc[i1] Posted messages 2603 Status Contributor 382
     
    Personally, I prefer tree for that.

    tree -d directory/


    --

    Archlinux ... A lightweight, fast, and KISS distribution on archlinux.fr
    4
  3. lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
     
    Salut,
    ls -lR | grep ^d 
    or
    find . -type d -exec ls -ld {} \; 
    --
    106485010510997108
    3
    1. jipicy Posted messages 40842 Registration date   Status Moderator Last intervention   4 898
       
      Hello,

      Or
      ls -R | grep '/$'
      ;-))

      --
      JP - Penguin breeder -
      Do something for the environment, close your windows and adopt a penguin.
      0
  4. artagon7 Posted messages 423 Status Member 7
     
    I tried your three commands and only the command from perlien at lami20j worked, which is ls -lR | grep ^d.

    In the case of the command proposed by jipicy (ls -R | grep '/$'), it doesn't display anything, while the command from marc[i1] doesn't work because there is no tree command in my shell!!! (I checked with man). As for the last two commands, it probably depends on the distribution we have.

    Thank you all three, :-)
    0
  5. jipicy Posted messages 40842 Registration date   Status Moderator Last intervention   4 898
     
    The command "tree" is not installed by default, that's why I didn't suggest it. However, I always install it on my systems... and the Martian does apparently too ;-)

    As for the command "ls -R | grep '/$'", indeed it depends on the distributions and the configuration of "ls" (alias to be precise in the form "alias ls='ls -F --color=auto'" for Mandriva for example), so you just need to write it with a patriotic flair for it to have the desired effect:
    ls -FR | grep '/$'
    ;-))

    --
    JP - Penguin breeder -
    Do a favor for the environment, close your windows and adopt a penguin.
    0
    1. lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
       
      ;-))
      --
      106485010510997108
      0
    2. marc[i1] Posted messages 2603 Status Contributor 382
       
      “The command "tree" is not installed by default, which is why I didn’t suggest it. However, I always install it on my systems... and apparently the Martian does too ;-)”

      hihi ^^

      --

      Archlinux ... A lightweight, fast, and KISS distribution on archlinux.fr
      0
  6. artagon7 Posted messages 423 Status Member 7
     
    Hello,

    Yes, indeed the command ls -FR | grep '/$' works. But ultimately it doesn’t give a very practical result, unlike the other command. For example, I have the directory /Informatique:

    /Informatique

    ../Linux
    ...../Fichiers_systemes
    ...../Pages_man

    ../Programmation
    ...../Langage_C
    ...../Scripts_bash

    ../Test
    ...../Test_1
    ...../Test_2
    ...../Test_3
    ...../Test_4


    If I run the command ls -FR | grep '/$', it gives me:

    $ ls -FR | grep '/$'
    Linux/
    Programmation/
    Test/
    Fichiers_systemes/
    Pages_man/
    Langage_C/
    Scripts_bash/
    Test_1/
    Test_2/
    Test_3/
    Test_4/


    In this case, I can still find my way. But in the case where I take a larger directory, I might get lost. In this case, is it possible to get something that looks like:

    /Informatique

    ../Linux
    ...../Fichiers_systemes
    ...../Pages_man

    ../Programmation
    ...../Langage_C
    ... etc ...

    I believe that the command from marc[i1] would be more appropriate, at least that’s what the name of the command suggests. If the command tree gives the result I want, I would like to install it. How do I install tree?

    Thank you,
    0
  7. bayrouni
     
    There is something simpler, and that's the job of ls.
    ls -d *
    0
    1. lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
       
      Hello,

      At least you tried?!
      The -d option does not serve to display only directories but
      -d, --directory
      displays directory names instead of their contents and does not dereference symbolic links



      lami20j@debian-acer:~/div$ ls -l total 4 drwxr-xr-x 2 lami20j lami20j 4096 Nov 30 17:09 aaa -rw-r--r-- 1 lami20j lami20j 0 Nov 30 17:09 toto.txt lami20j@debian-acer:~/div$ ls -d * aaa toto.txt lami20j@debian-acer:~/div$ ls -d */ aaa/



      The -d option does not serve to display only directories but (man ls)
      -d, --directory
      displays directory names instead of their contents and does not dereference symbolic links

      Here’s what happens when I use -d with -R
      lami20j@debian-acer:~/div$ ls -lRd * drwxr-xr-x 2 lami20j lami20j 4096 Nov 30 17:11 aaa -rw-r--r-- 1 lami20j lami20j 0 Nov 30 17:09 toto.txt lami20j@debian-acer:~/div$ ls -lR * -rw-r--r-- 1 lami20j lami20j 0 Nov 30 17:09 toto.txt aaa: total 0 -rw-r--r-- 1 lami20j lami20j 0 Nov 30 17:11 zzz.png lami20j@debian-acer:~/div$ 

      or
      lami20j@debian-acer:~/div$ ls -l aaa total 0 -rw-r--r-- 1 lami20j lami20j 0 Nov 30 17:11 zzz.png lami20j@debian-acer:~/div$ ls -ld aaa drwxr-xr-x 2 lami20j lami20j 4096 Nov 30 17:11 aaa lami20j@debian-acer:~/div$ 
      0
    2. algerien
       
      There is an easier way! Just the directories that contain files '.' so
      echo /*/.
      0
  8. bayrouni
     
    Yes indeed.
    The --directory option is more subtle than I thought.

    Thank you for this clarification.
    0
  9. lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
     
    Hello,

    as such, from the beginning you should tell us what display you want to get ;-)
    in this case, indeed the command given by marc[i1] is the solution

    if I'm not mistaken you are on ubuntu
    sudo aptitude install tree

    --
    106485010510997108
    -1
    1. artagon7 Posted messages 423 Status Member 7
       
      Yes, lami20j I should have specified earlier. I take note of the lesson. The command tree gives exactly what I wanted.

      Thank you and best regards, :-)
      0