The Sourcerer's Apprentice

The adventures of David Heinemann in IT & software development

dirstats.sh

| Comments

I often find myself using du -hcd1 and wc -l to count the number of files and folders within a directory and calculate its total disk usage, especially when archiving a website, so I knocked up a small shell script to make it a little easier.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/sh
    #Prints a directory's total disk usage and amount of sub-files &
    #folders.
    if [ -z $1 ]
    then
        echo "Please specify a directory."
    else
        if [ -d $1 ]
        then
            disk=`du -hcd1 $1 | awk '/total/ {print $1}'`
            lines=`tree $1 | wc -l`
            echo "$1"
            echo "Contains $lines files and folders."
            echo "Consumes $disk of space."
        else
            echo "$1 is not a directory."
        fi
    fi

The output looks like this:

1
2
3
4
[david@Fnord ~]$ sh dirstats.sh somefolder
somefolder
Contains 4395 files and folders.
Consumes 38M of space.

Download at Pastebin.

Comments