Views:
1,812,988β
Votes: 3β
Tags:
filesystem
command-line
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/1320936
Title:
How do I determine the total size of a directory (folder) from the command line?
ID:
/2021/03/03/How-do-I-determine-the-total-size-of-a-directory-_folder_-from-the-command-line_
Created:
March 3, 2021
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
Iβm conditioned to the ll
command which is aliased to ls -alF
. It is just missing a file count and size of files at the bottom. I played with du
and tree
but could not get the totals I needed. So I created lll
to do that for me.
In your ~/.bashrc
place the following:
lll () {
ls -alF "$@"
arr=($(ls -alF "$@" | awk '{TOTAL+=$5} END {print NR, TOTAL}'))
printf " \33[1;31m ${arr[0]}\33[m line(s). "
printf "Total size: \33[1;31m ${arr[1]}\33[m\n"
# printf "Total size: \33[1;31m $(BytesToHuman <<< ${arr[1]})\33[m\n"
}
Save the file and resource it using . ~/.bashrc
(or you can restart your terminal).
Sample output
The nice thing about ll
output is itβs colors. This is maintained with lll
but lost when using find
or du
:
TL;DR
A bonus function you can add to ~/.bashrc
is called BytesToHuman()
. This does what most console users would expect converting large numbers to MiB, GiB, etc:
function BytesToHuman() {
# https://unix.stackexchange.com/questions/44040/a-standard-tool-to-convert-a-byte-count-into-human-kib-mib-etc-like-du-ls1/259254#259254
read StdIn
b=${StdIn:-0}; d=''; s=0; S=(Bytes {K,M,G,T,E,P,Y,Z}iB)
while ((b > 1024)); do
d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
b=$((b / 1024))
let s++
done
echo "$b$d ${S[$s]}"
} # BytesToHuman ()
Next flip the comment between two lines in lll ()
function to look like this:
# printf "Total size: \33[1;31m ${arr[1]}\33[m\n"
printf "Total size: \33[1;31m $(BytesToHuman <<< ${arr[1]})\33[m\n"
Now your output looks like this:
As always donβt forget to re-source with . ~/.bashrc
whenever making changes. (Or restart the terminal of course)
PS - Two weeks in self-quarantine finally gave me time to work on this five year old goal.