Print the size of everything in the system (Starting at /) in bytes:
echo $(($(find / -print “+%s”)))

Print the size of everything in the system (Starting at /) in Kbytes:
echo $(($(find / -print “+%s”))) | awk ‘{print $1/1024 KB}’

Print the size of everything in the system (Starting at /) in Mbytes:
echo $(($(find / -print “+%s”))) | awk ‘{print $1/1024/1024 MB}’

Print the size of everything in the system (Starting at /) in Gbytes:
echo $(($(find / -print “+%s”))) | awk ‘{print $1/1024/1024/1024 GB}’

Print the size of everything in the system (Starting at /) in Tbytes:
echo $(($(find / -print “+%s”))) | awk ‘{print $1/1024/1024/1024/1024 TB}’

Explanation:

‘Find’ can be used to select any or all files (files/directories, remember in linux everything is a file). Its selection mechanism has alot of variables, so after reading the man page or playing around with find (or seeing peoples find tutorials – such as my find tutorial – just search for ‘find’ using my sites function). For example I can tell ‘find’ to select all movies save on my harddrive no longer then 1 year ago and definitely more then 6 months ago -that reside in only the folder /movies and a maximum of 3 subfolders deep. Then with that find operation I can choose to delete or rename or maybe archive them all, but for simplicity sake I can just have each found file print me its size in bytes, then I can use bash to add up the numbers and then I can use echo to print the result.

Bash simple integer math (this is bash integer math, it doesn’t do decimal or fractions – so 5/3 would give you 1 not 1.666):
$((1+2)) this does nothing
K=$((5-3)) this saves 5-3 which is 2 into the K variabke
echo  $((1+2) this prints 3 on the screen
echo $K this prints 2 on the screen

find / -printf “,%s” this prints every files in the system size in bytes out seperated by commas
find / -print “+%s” this prints every files size out in bytes seperated by + signs -> which can be used with bash math to give us the total size of everything

$((find / -print “+%s”)) this wont work, because we are not telling bash to run the find command, this makes bash your trying to do math with words and characters – so it will error on you

This how ever works
$((`find / -print “+%s”`))

Also this works
$(($(find / -print “+%s”)))

Command Substitution
`command` and $(command) are the same thing, both and $() execute the command inside and passon the outcome to whatever is holding the or $()

popular example with $() and “:
dpkg -S program-exact-location
dpkg -S /bin/bash
dpkg -S bash this fails i need a full location
which bash this give me a full location, so how do I use its output in dpkg -S
dpkg -S `which bash` or dpkg -S $(which bash) both work

Anyhow back to topic, this $(($(find / -print “+%s”))) or the other version of it. Dont output anything to the screen, but we know echo will (or we could save it to a variable for later use):

echo $(($(find / -print “+%s”)))

or

SIZES=$(($(find / -print “+%s”)))
echo ${SIZES}
or
echo $SIZES

Recap of bash command substitution and bash math and simple variables:
So to get your head around the bash parenthesis and curly braces uses. Here is a summary of all of it covered:
`word` – this runs the command called word
$(word) – this runs the command called word as well
${word} – this means word is a  variable and it will give you its value in return
$word – this means word is a variable and it will give you its value in return as well
$((word)) – this will fail as that is not math however if your word was some math then it would do it
$((1+3+4+5) – this would return the sum of 1,3,4, and 5 which is 13. This will return 13.

Leave a Reply

Your email address will not be published. Required fields are marked *