Shell Script To Count Number Of Files In Each Subdirectories
Posted in Decision Making » File-management
#!/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 [ "$d" != "." -a "$d" != ".." ] && echo "$d dirctory has $(ls -l $d | wc -l) files" || : done
Download - Email this to a friend - Printable version
Is your site working? Monitor Your Web Site 24/7. Get SMS alerts on server downtime! Free 30-day trial including 20 SMS!
Related Other Helpful Shell Scripts:
- Shell script to find the number of files present in the current directory without using WC command
- Shell Script To Combine Any Three Text Files Into a Single File
- Shell Script To Count Vowels, Blank Spaces, Characters, Number of line and Symbols
- Shell Script To Search and Count A Single Word Pattern Recursively In A UNIX File
- Shell Script Searches For A Single Word Pattern Recursively
Discussion on This Shell Script:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: conditional expression, conditional expressions, dirctory, eq, exit 1, fi, find command, for loop, home directory, loop method, shell loops, start 1 ~ Last updated on: September 19, 2008


GNU find/awk
find /path -printf "%h:%f\n" | awk -F: '{d[$1]++}END{ for(o in d) print d[o],o }’
Even easier-
find -type f -execdir pwd \; | sort | uniq -c