LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   List total number of directories (https://www.linuxquestions.org/questions/linux-newbie-8/list-total-number-of-directories-313490/)

paraiso 04-15-2005 06:20 AM

List total number of directories
 
Hi!

Could someone tell me how to list the total number of directories I would have for instance in my home directory?

Thank you in advance

ToniT 04-15-2005 06:39 AM

Code:

find -type d | wc -l

paraiso 04-15-2005 10:27 AM

Terve ToniT!

Thank you for your quick reply. I have tried find -type d | wc -l and got the number 11 but when I do a ls -F to verify how many directories I have, I only see two directories. How it comes that the command find -type d | wc -l gives me 11 ?

Regards

paraiso 04-15-2005 10:42 AM

Oups! sorry I didn't notice the subdirectories. So I have not 2 directories but 12 and when I do the command find -type d | wc -l the output is 13. So it's counting also the directory I am currently in. Is there a way to ignore this directory ?

Regards

ToniT 04-15-2005 11:24 AM

Code:

find -type d | wc -l | awk '{print $1-1}'
;)

You can try those commands separatedly.
  • Find is used to find files or directories. Argument "-type d" limits search to only diretories.
    Typing just
    Code:

    find -type d
    gives you a listing of those directories.
  • wc is an acronym from word count. It is used to count bytes, words or lines.
    Parameter "-l" means to show only number of lines in the input (which was this case the output of previous command). You can test this eg. by writing
    Code:

    wc
    , typing something and hitting ctrl-d (means end of buffer) at the end and examine the output.

    Other ways to test it is like
    Code:

    cat /etc/inittab | wc
    (cat is used to type files to output[generally screen, in this case as input to wc]).
  • The last command is awk, a small programming language called with a command '{print $1-1}', which means that for every line in the input, print the first field(a section in the line separated by space or enter) minus one.

    You can test this by writing
    Code:

    awk '{print $1-1}'
    and typing some numbers. (hit ctrl-d to end)


You might want to also try commands
Code:

man find
,
Code:

man wc
and
Code:

man awk
.

Also typing
Code:

info coreutils
gives you a good overview of the toolbox that
is available to the shell.

paraiso 04-18-2005 04:04 AM

Thanks a lot ToniT!

That was a great and well explained tutorial ! it’s uncredible how powerful shell commands can be.


All times are GMT -5. The time now is 08:35 PM.