The usefulness of bash scripting.


##  One of the software engineers in our company 
##  spent the better part of a day writing a C++ program
##  that would look through the directories of all the users 
##  and add up the space they were using and make a listing of the results. 
##  Three lines of code in bash do the same\!

^ I don’t know where I read this once upon a time and in all likelihood I’ve already mentioned or written about this script before …!?

Anyway! 😆

Here’s the script:

#!/bin/bash
# Modified: Today by E.L.F.
#
## Licensed as:
## http://www.opensource.org/licenses/mit-license.php
#
## Script-name - space.report.sh
#
##  One of the software engineers in our company 
##  spent the better part of a day writing a C++ program
##  that would look through the directories of all the users 
##  and add up the space they were using and make a listing of the results. 
##  Three lines of code in bash do the same\!
#
##  Root check / Sanity check.  You decide.  >-)
##  Just in case someone calls this script with sudo -s,
##  or has cd'ed into his/her path.
#
set -e
echo
if [ "$USER" = root ];then echo -e "\tError: \""$USER"\" isn't allowed to use this script!";exit 0
else echo -e "\t${USER} may proceed.\n\tMay peace be with you.\n";fi
## ^ Comment the above 2 lines out, if root is required!
if [ -n "$Color_Test" ];then echo -e ${Magenta}"\tOk, colors are defined."${Nc}
else
Black="\033[0;30m"
Red="\033[0;31m"
Green="\033[0;32m"
Yellow="\033[1;33m"
Blue="\033[1;34m"
Magenta="\033[1;35m"
Cyan="\033[1;36m"
White="\033[1;37m"
Nc="\033[0m" # No Color
echo -e ${White}"\tIf they weren't, then now they are."${Nc}
fi
clear
Date=$(date +"%F") # Renders for me as yyyy-mm-dd\!
SR=space.report_"$Date".txt
du -sh * | sort -nr > "$SR"
echo "" >> "$SR"
du -sh ./.* | sort -nr >> "$SR"
echo "" >> "$SR"
df -h >> "$SR"
echo "" >> "$SR"
echo "This space report was made on: "$Date"" >> "$SR"
exit 0

Now for the astute among you, who saw all these ${color} codes in the script.

And here’s today’s result, it took about 5 seconds to complete! 😆

962M	Pictures
435M	Pictures - GT-N7000
173M	Documents
159M	Downloads
129G	Videos
110M	Ubuntu One
64K	bookmarks.html
20G	Music
8.0K	Desktop
5.4M	Public
4.0K	Templates
0	*.tor-relay.thruhere.net
0	space.report_2012-12-18.txt
0	PDF
0	learned_words.txt
0	ignored_words.txt

748M	./.cache
712K	./.gstreamer-0.10
616K	./.dvdcss
596K	./.compiz
536K	./.recently-used.xbel
324K	./.xsession-errors
317M	./.thumbnails
260K	./.icedteaplugin
247M	./.electricsheep
223M	./.kde
209M	./.mozilla
207M	./.local
195M	./.usr
192M	./.config
168K	./.pulse
159G	./..
159G	./.
152K	./.omegat
152K	./.macromedia
148K	./.dvdrip
120K	./.athena.face
112K	./.config-2.6.32-25-generic
96K	./.icedtea
92K	./.cddb
88K	./.face.png
68K	./.xine
68K	./.audacity-data
60K	./.avidemux
56K	./.ICEauthority
52K	./.pki
52K	./.k3d
48K	./.subversion
40K	./.filezilla
36K	./.sudoku
36K	./.qmmp
32K	./.openshot
32K	./.FBReader
32K	./.evolution
32K	./.dvdriprc
28K	./.winff
24K	./.ssh
24K	./.Molon.Labe
21M	./.themes
21M	./.openoffice.org
20K	./.adobe
16K	./.gegl-0.0
16K	./.gconfd
16K	./.dbus
16K	./.bash_functions
12K	./.torrc
12K	./.qt
12K	./.netx
12K	./.mission-control
12K	./.bash_aliases
8.0K	./.update-notifier
8.0K	./.synaptic
8.0K	./.putty
8.0K	./.mplayer
8.0K	./.lyrics
8.0K	./.hplip
8.0K	./.DontDoWindows.png
8.0K	./.bashrc.jic
8.0K	./.bashrc
7.3M	./.icons
5.1G	./.bitcoin
4.2M	./.Skype
4.0K	./.xscreensaver-getimage.cache
4.0K	./.xinput.d
4.0K	./.Xauthority
4.0K	./.windows-serial
4.0K	./.wifi.txt
4.0K	./.thunderbird
4.0K	./.TarBalls
4.0K	./.selected_editor
4.0K	./.rnd
4.0K	./.recently-used
4.0K	./.pulse-cookie
4.0K	./.profile
4.0K	./.printer-groups.xml
4.0K	./.nautilus
4.0K	./.nano_history
4.0K	./.mkvmergeGUI
4.0K	./.kderc
4.0K	./.htoprc
4.0K	./.gtk-recordmydesktop
4.0K	./.gtkrc-2.0-kde4
4.0K	./.gtk-bookmarks.R3Q1CW
4.0K	./.gtk-bookmarks
4.0K	./.gnome2_private
4.0K	./.fonts.conf
4.0K	./.exclude.txt
4.0K	./.examples.desktop
4.0K	./.esd_auth
4.0K	./.dmrc
4.0K	./.directory
4.0K	./.btrfs
4.0K	./.BOINC Manager
4.0K	./.bash_logout
4.0K	./.bash_history
3.2M	./.gnupg
2.4M	./.fonts
2.0M	./.gconf
2.0M	./.fontconfig
1.5M	./.gimp-2.6
1.4M	./.xsession-errors.old
1.4M	./.recently-used.xbel.RYNEBW
1.1M	./.gnome2
0	./.www.kiwix.org
0	./.sudo_as_admin_successful
0	./.mozilla-thunderbird
0	./.gvfs
0	./.gksu.lock

Filesystem            Size  Used Avail Use% Mounted on
/dev/sdb3              16G  8.5G  7.2G  55% /
none                  2.0G  380K  2.0G   1% /dev
tmpfs                 2.0G  7.0M  2.0G   1% /dev/shm
none                  2.0G  212K  2.0G   1% /var/run
none                  2.0G     0  2.0G   0% /var/lock
none                  2.0G     0  2.0G   0% /lib/init/rw
/dev/mapper/vg-home   9.2G  4.4G  4.8G  48% /home
/dev/mapper/vg-var    3.8G  1.5G  2.3G  41% /var
/dev/mapper/vg-linkedin
                      263G  205G   56G  79% /vault
/dev/mapper/cryptotmp
                     1012M  7.5M  953M   1% /tmp

This space report was made on: 2012-12-18
Tiny Matroska pinguins

Tiny Matroska pinguins (Photo credit: Act, don’t think)


That’s all folks! 😆

Kindest regards,

Alex

ღ Ƹ̵̡ Ӝ̵̨̄ Ʒ ☆ ˜ ” * ° • . ¸ł¸ . • ° * ” ☆ ★ ☆ ę ˜

♫ d(。◕‿◕。)b ♪♪

Use the contact-form below, for feedback purposes.







Back to top.