updated to specific CRONUSER variable
[feisty_meow.git] / scripts / files / count_files.sh
1 #!/bin/bash
2
3 # counts the number of files in a set of directories.
4 # if no directories are provided, uses the current working directory.
5
6 # make sure they gave us some arguments, or go with our default of the current dir.
7 #hmmm: could use the count notation instead of sloppy empty check.
8 if [ -z "$1" ]; then
9   # reset the arguments list to scan any directories found in cwd.
10   SAVEIFS="$IFS"
11   IFS=$(echo -en "\n\b")
12   set -- $(find . -maxdepth 1 -mindepth 1 -type d) "${@:2}"
13   IFS="$SAVEIFS"
14 fi
15
16 # run through all the parameters provided and find any directories under them
17 # (or probably barf if they're not dirs).
18 #hmmm: really?  "barf" is an implementation strategy now?
19 for countfilesname in "${@}" ; do
20 #  echo "arg is: '$countfilesname'"
21   # print the count of files followed by directory name,
22   # with leading zeros to allow sorting, which get
23   # redigested as spaces before showing the list.
24   printf "%06d -- %s\n" $(find "$countfilesname" -type f | wc -l) "$countfilesname"
25 done |
26   # provide sorted output based on how many files exist
27   # in each directory.
28   sort -r |
29   # eat the zeroes but keep the tabular look (i.e. replace each leading zero 
30   # with a space).  had to do it as cases, since this seems like context-
31   # sensitive matching, which sed will not do, i think).
32   sed -e 's/^000000/     0/' -e 's/^00000/     /' -e 's/^0000/    /' \
33       -e 's/^000/   /' -e 's/^00/  /' -e 's/^0/ /'
34