Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Anyone know of a script that gives me a count of the files under all the directories under a certain directory? Thanks, Klesla...
  1. #1
    Just Joined!
    Join Date
    Feb 2005
    Posts
    2

    Count Files under multiple directories

    Anyone know of a script that gives me a count of the files under all the directories under a certain directory?

    Thanks,
    Klesla

  2. #2
    Linux Engineer
    Join Date
    Nov 2004
    Location
    Montreal, Canada
    Posts
    1,267
    This His the way to get the number of file under the current directory...

    As for all the subdirectory, since I never tryed it, I'll write a little bash file that does this... (when I get home)
    \"Meditative mind\'s is like a vast ocean... whatever strikes the surface, the bottom stays calm\" - Dalai Lama
    \"Competition ultimatly comes down to one thing... a loser and a winner.\" - Ugo Deschamps

  3. #3
    Just Joined!
    Join Date
    Feb 2005
    Posts
    2
    Thanks. I found a way on the net for counting in the current directory.

    ll -1A|wc -l

    But I can't seem to have it give me results for all the sub directories and there file count. Something that can tell me the directory and what count is under it.

  4. #4
    Linux Engineer
    Join Date
    Nov 2004
    Location
    Montreal, Canada
    Posts
    1,267
    That probably cant be done using simple bash command... I dont think its "that" powerfull...

    Using C++ or Java, would make this an easy task thou
    \"Meditative mind\'s is like a vast ocean... whatever strikes the surface, the bottom stays calm\" - Dalai Lama
    \"Competition ultimatly comes down to one thing... a loser and a winner.\" - Ugo Deschamps

  5. #5
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    Quote Originally Posted by UgoDeschamps
    Using C++ or Java, would make this an easy task thou
    Or even simpler using something like Perl or Python:
    Code:
    #!/usr/bin/env python
    
    import os, sys
    
    def numOfFiles(dir):
      count = 0
      for tup in os.walk(dir): count += len(tup[2])
      return count
    
    for arg in sys.argv[1:]:
      print arg, '->', numOfFiles(arg)
    If you insist on using bash, it could be possible to use -R -1 flags and strip blank lines together with those ending with colons (subdirs). Look for pattern mathing in the bash manual page.
    Hope you'll handle the problem soon, best regads.
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  6. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    I thought this might be a homework question so I didn't
    answer earlier but since a script has been posted already
    here's my effort using C-shell:
    Code:
    set a = "`find . -print0`" ; echo $#a
    This assuming that the current directory is the one we're
    interested in.Otherwise replace the . (dot) after find by
    the name of the directory.The count will include the head
    directory.If you don't want that , subtract 1 from the value of $#a

  7. #7
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    Quote Originally Posted by Santa's little helper
    I thought this might be a homework question so I didn't
    answer earlier but since a script has been posted already
    here's my effort using C-shell:
    Code:
    set a = "`find . -print0`" ; echo $#a
    This assuming that the current directory is the one we're
    interested in.Otherwise replace the . (dot) after find by
    the name of the directory.The count will include the head
    directory.If you don't want that , subtract 1 from the value of $#a
    It seems to count the number of everything in the current directory, but the question is about files. Or am I wrong?
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  8. #8
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    When you say files do you mean just regular files ? I assumed the
    question was about all kinds of files ie regular files , links , directories etc.

    But anyway if one wants to exclude directories say they could write
    Code:
    set a = "`find . \! -type d -print0`" ; echo $#a
    Or to exclude directories and links
    Code:
    set a = "`find . \! -type l \! -type d -print0`" ; echo $#a

  9. #9
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Oh , yeah , forgot to say that a solution like ls -l | wc -l or
    similar will only give the correct result only if no file name contains a newline character.This is almost certain but not 100% .

  10. #10
    Just Joined!
    Join Date
    Mar 2005
    Posts
    1

    here is the command line answer

    ls -laR | wc -l


    will recursivly count files

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...