Little Girl's Mostly Linux Blog

DirectoryCheck

Directory check

This page was last updated on January 20, 2017.

Here are two scripts that let you know if a directory is empty or not. The first one uses the command line to notify you and the other one uses KDialog (for KDE) to pop up a window to notify you. Both scripts will play the system bell if the directory is not empty.

Plain command line script
#!/bin/bash
#######################################################
# This script checks a directory's size and lets you  #
# know if the directory is empty or not. The system   #
# bell will be played if the directory is not empty.  #
#                                                     #
# BEFORE YOU BEGIN:                                   #
# 1. Open this file in a text editor.                 #
# 2. Find this line in the script below:              #
#    MYTARGET=/home/username/tmp                      #
# 3. Change /home/username/tmp to the path of the     #
#    directory you'd like this script to check.       #
# 3. Save the file and exit the text editor.          #
#                                                     #
# HOW TO RUN THIS SCRIPT:                             #
# 1. Open a terminal window.                          #
# 2. Change to the directory this script is in.       #
# 3. Type this command to run the script:             #
#    bash DirectoryCheck                              #
# 4. Press the Enter key.                             #
#                                                     #
# HOW TO RUN THIS SCRIPT AND LOG THE RESULTS:         #
# 1. Open a terminal window.                          #
# 2. Change to the directory this script is in.       #
# 3. Type this command to run the script:             #
#    bash DirectoryCheck | tee -a DirectoryCheck.log  #
# 4. Press the Enter key.                             #
#                                                     #
#######################################################
MYTARGET=/home/username/tmp
CALCULATESIZE=$(du -s $MYTARGET);
DIRECTORYSIZE=$(echo $CALCULATESIZE | awk '{ print $1 }');
echo "Directory checked on $(date):";
if [ $DIRECTORYSIZE -le 4 ] ; then
	echo "The directory is empty!";
elif [ $DIRECTORYSIZE -gt 4 ] ; then
	echo -e "\a" >&2;
	echo "The directory is not empty!";
fi;
exit;
KDialog script
#!/bin/bash
#######################################################
# This script checks a directory's size and lets you  #
# know if the directory is empty or not. The system   #
# bell will be played if the directory is not empty.  #
#                                                     #
# BEFORE YOU BEGIN:                                   #
# 1. Open this file in a text editor.                 #
# 2. Find this line in the script below:              #
#    MYTARGET=/home/username/tmp                      #
# 3. Change /home/username/tmp to the path of the     #
#    directory you'd like this script to check.       #
# 3. Save the file and exit the text editor.          #
#                                                     #
# HOW TO RUN THIS SCRIPT:                             #
# 1. Open a terminal window.                          #
# 2. Change to the directory this script is in.       #
# 3. Type this command to run the script:             #
#    bash DirectoryCheck                              #
# 4. Press the Enter key.                             #
#                                                     #
# HOW TO RUN THIS SCRIPT AND LOG THE RESULTS:         #
# 1. Open a terminal window.                          #
# 2. Change to the directory this script is in.       #
# 3. Type this command to run the script:             #
#    bash DirectoryCheck | tee -a DirectoryCheck.log  #
# 4. Press the Enter key.                             #
#                                                     #
#######################################################
MYTARGET=/home/username/tmp
CALCULATESIZE=$(du -s $MYTARGET);
DIRECTORYSIZE=$(echo $CALCULATESIZE | awk '{ print $1 }');
echo "Directory checked on $(date):";
if [ $DIRECTORYSIZE -le 4 ] ; then
	kdialog --msgbox "The directory is empty!";
elif [ $DIRECTORYSIZE -gt 4 ] ; then
	echo -e "\a" >&2;
	kdialog --error 'The directory is not empty!';
fi;
exit;


Obligatory Happy Ending

And they all lived happily ever after. The end.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

Create a free website or blog at WordPress.com.