Issue
I am building a simple caching script in bash.
Let us assume we have the folders
source
dest
I want to write a if condition and check if the total size of source matches the total size of dest (by total size I mean it includes also the subfolders subfiles recursively everything), then do some action.
The script will look something like:
sourceSizeInBytes=2; // dont know how to get only total in bytes
destSizeInBytes=2; // dont know how to get only total in bytes
if [ $sourceSizeInBytes -eq $destSizeInBytes ]
then
echo "Both folders have equal size. I will do something";
else
echo "folders are not of the same size";
fi
du -sch gives not just a number but more information.
How can I write such a condition in one reliable way ?
Solution
As suggested by @Charles Duffy in the comments, if you're using GNU bash
, below code is more suitable:
read sourceSizeInBytes _ < <(du -s --bytes source)
read destSizeInBytes _ < <(du -s --bytes dest)
if (( sourceSizeInBytes == destSizeInBytes ))
then
echo "Both folders have equal size. I will do something"
else
echo "folders are not of the same size"
fi
Answered By - ashish_k Answer Checked By - Cary Denson (WPSolving Admin)