Directory check
This page was last updated on July 30, 2011.
Here are two scripts that let you know if a directory is empty or not. Both scripts will play the system bell if the directory is not empty.
Plain command line script
- This script uses the command line to notify you:
#!/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
- This script uses KDialog (for KDE) to pop up windows to notify you:
#!/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.
