Bash Scripting For Buffoons - Snapshot Script

Ceri Shaw
02/28/16 04:13:55AM
@ceri-shaw

Snapshot Script



⇦ Back to 'Bash Scripting For Buffoons'

This script  utilises the 'du', 'ls' and 'df' commands to provide a mass of information about the folder in which it is located. The 'df-h' command also gives you a measure of your current disk usage similar to the following:-

Filesystem   Size  Used Avail Use% Mounted on
/dev/hda1     46G   31G  108G  23% /

Note that the script is written in such a way that 'ls -lhSR' and 'du -ah/sh' only work with reference to the folder in which they are located. Do not be alarmed when your console shuts down after the script has run. The amazingly useful 'tee' command has generated a file called 'snapshot' in your current working folder. You will find the output from the command in that file. This script is perhaps a prime candidate for further sophistication and development. At one time I wrote a version that would preserve the current and previous outputs and compare them with the diff command. If you go down that route you can have fun playing with the various output formats that 'diff' provides, for instance, obsessively generating and checking output every two seconds to see if it's somehow different ( which is unlikely ) . I stopped playing with this when I realised that it did nothing that I couldn't accomplish more easily in the GUI. On a 'headless' server though it might still be of some value.

#!/bin/bash
snap ()
{
date
echo " "
echo "Snapshot" ;
echo "---------" ;
echo " " ;
echo "ls -lhSR --authorship /home/userone [Showing permissions , size , authorship and last access times of all files and folders]" ;
echo "----------------------------------------------------" ;
echo "  " ;
ls -lhSR --author ;
echo " " ;
echo "du -sh /home/userone [Directory size - Human readable]" ;
echo "----------------------------------------------------" ;
echo "  " ;
du -sh ;
echo " " ;
echo "du -ah /home/userone [Showing sub-directory structure and file size inc. hidden files]" ;
echo "----------------------------------------------------" ;
echo "  " ;
du -ah ;
echo " " ;
echo "df -h /home/userone [Showing amount of disk space on /home partition]" ;
echo "----------------------------------------------------" ;
echo "  "
df -h /home ;
}

snap | tee /home/userone/snapshot

gedit /home/userone/snapshot

exit 0
 

NOTE: In the likely event that your username is not 'userone' you will need to amend the last two lines accordingly.