Find the answer to your Linux question:
Results 1 to 3 of 3
The following script seems to work well: for f in `ls -R | grep ":" | sed "s/://"`; do echo $f `ls $f | wc -l`; done The only limitation ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    1

    List file count in subdirectories - one line shell script

    The following script seems to work well:
    for f in `ls -R | grep ":" | sed "s/://"`; do echo $f `ls $f | wc -l`; done

    The only limitation is that it will report errors for files containing a colon ":".

    David Rodecker
    Getting Local Businesses Online
    localsplash.com

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by drodecker View Post
    The following script seems to work well:
    for f in `ls -R | grep ":" | sed "s/://"`; do echo $f `ls $f | wc -l`; done

    The only limitation is that it will report errors for files containing a colon ":".

    David Rodecker
    Getting Local Businesses Online
    localsplash.com
    this is an old thread.

    issues with your method
    1) for loop with ls -R breaks on files with spaces in their names
    2) why use grep ":" and sed together , when sed is supposed to find ":" also? therefore grep is redundant
    3) echo $f `ls $f | wc -l` <--- what if $f is a file instead of a directory?

  3. #3
    Just Joined!
    Join Date
    May 2009
    Posts
    6
    better way is : find "directory name" -type f |wc -l

Posting Permissions

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