I need a script that counts the files in a directory (and sub). I've taken the followed script, and changed it to my need.
It works like it should, except for folders with space characters. I'm pretty sure I'm missing quotes anywhere, but can't figure it out yet.
Additional Informations
- Linux 2.6.22.19-0.4-default (this server is not in a productive environment anymore.)
- GNU find version 4.2.31
- I'm not able to rename the directories.
Example Directory Structure
.
..
01_infos
02_sent
03_inbox
04_public and private
197.
145.
329.
13.
Script
#!/bin/bash
# Write a script that will count the number of files in each of your subdirectories.
# -------------------------------------------------------------------------
# Copyright (c) 2001 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
START=$HOME
# change your directory to command line if passed
# otherwise use home directory
[ $# -eq 1 ] && START=$1 || :
if [ ! -d $START ]
then
echo "$START not a directory!"
exit 1
fi
# use find command to get all subdirs name in DIRS variable
DIRS=$(find "$START" -type d)
# loop thought each dir to get the number of files in each of subdir
for d in $DIRS
do
echo "$d directory has $(find "$d" -maxdepth 1 -regex '.*\.' -type f | wc -l) files" || :
done
output
./01_infos directory has 1 files
./02_sent directory has 9 files
./03_inbox has 4 files
find: ./04_public: No such file or directory