//View Tip #502
Similar Tips
» Loop over files with spaces in the name
» Bash efficiency formula ;-)
» Local/remote webserver
» Check low space
» Directories and its size

 

Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
Counts files in the current directory and subdirectory
alias lll='for i in *; do echo "`ls -1aRi  $i | awk "/^[0-9]+ / { print $1 }" | sort -u | wc -l` $i" ; done | sort -n'

found is vserver-tools


View Comments »




Comments 

Add your comment

Comments are currently disabled
That fails on some systems where there are files or directories with spaces in their names.

Try instead:

alias lll='find . | wc -l'
Posted 2009-02-03 21:05:31
if you want only the file count and not the directories stick to:

alias lll='find . -type f | wc -l'
Posted 2009-02-04 01:24:35
unfortunately that also has a problem with filenames containing newlines (which are valid) in any case Techfun's implementation is not the same as the first one, Techfun's gives a total of the files while the original provides counts per directory.

this, as usual, can be better and more efficiently implemented by the shell alone:

 lll() (
     f() {
         local files=0 dirs=()
         for dir_entry in "$1"/*; do
             [[ -d $dir_entry ]] && dirs+=("$dir_entry") || let "files++"
         done
         echo "$files $1"
         for dirname in "${dirs[@]}"; do
             f "$dirname"
         done
     }
     shopt -u failglob dotglob
     shopt -s nullglob
     f "${1:-$PWD}"
 )

notice the use of parenthesis instead of braces on the outer body to avoid corrupting the environment by changing the glob settings.
Posted 2009-02-13 22:21:44

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit