If you have a hosting, you usually have limited disk capacity on it. Some times there are file managers in the hosting control panel, some times not. Here I will explain how to get disk space used, if your hosting control panel has no file manager. | So, each GNU system always has two built-in commands to determine the disk and file space usage. They are 'du' and 'df'. Because most hostings working with GNU based operating systems like BSD or Linux, we have a right to suppose, that we have ssh login and aforementioned commands available on it. If it's so on your hosting, read further
The df command is vain for us, because we don't must take care about the whole server machine. The du command is on the contrary useful for us - with it we can find big files and folders and archive them, etc. So, this is the example output of du:
BASH:
user@host:~$ du -h ~/past/
107M /home/user/past/fr
195M /home/user/past/
What was for me the inconvenience - the output is mostly not clearly. Especially if many sub folders are there. Yes, one can do some filters (or as I have recently experienced, sort with pipe are usably too), but what was my idea - I want get all files with size for example greater than 4Mb. And so I have also built the script.
BASH:
#!/bin/bash
###############################
# @file maxfsize #
# @author Anatoliy Belsky #
# @license GPL #
###############################
#############
# help output
#############
if [ $# -lt 2 ];
then
echo "usage: `basename $0` /path maxfilesize
Find all files in the path with size greater than maxfilesize specified
examples:
`basename $0` / 30K
`basename $0` /usr 30M
If no k, m or g letter with size given, means bytes.";
exit 2;
fi
###########################################
# checking size units and setting up values
###########################################
# options for du
DUOPTS="-b -c -S -x"
# size multiplier (m, g, k or none)
FSIZELABEL='';
# corresponding size multiplier number
FACTOR=1;
# given file size param
MAXFSIZE='';
COUNT=0;
while [ "$COUNT" -lt ${#2} ];
do
CH=${2:$COUNT:1};
case "$CH" in
[0-9])
MAXFSIZE="$MAXFSIZE$CH";
;;
[Kk])
FSIZELABEL="k";
FACTOR=1024;
;;
[Mm])
FSIZELABEL="M";
FACTOR=$(echo 1024*1024 | bc);
;;
[Gg])
FSIZELABEL="G";
FACTOR=$(echo 1024*1024*1024 | bc);
;;
esac;
COUNT=$(echo $COUNT+1 | bc);
done;
typeset -i MAXFSIZE && MAXFSIZE=$(echo $MAXFSIZE*$FACTOR | bc);
################################
# searching and producing output
################################
du $DUOPTS "$1" | while read LINE
do
LINE=`tr "\t" ' ' <<< "$LINE"`;
COUNT=0;
# get filesize
FSIZE='';
while [ "$COUNT" -lt "${#LINE}" ];
do
CH=${LINE:$COUNT:1};
case "$CH" in
[0-9]) FSIZE="$FSIZE$CH";;
' ') break;;
esac;
COUNT=$(echo $COUNT+1 | bc);
done;
# get filepath
FNAME=${LINE:$COUNT:${#LINE}};
# evaluate
if [ $FSIZE -gt $MAXFSIZE ];
then
echo "$(echo $FSIZE/$FACTOR | bc)$FSIZELABEL $FNAME";
fi
done;
Note, that things like "${str:2:1}" or "tr "\t" ' ' <<< $var" may not work on older bash versions or on some sh versions on some operating systems like for example FreeBSD 6.1 I've tried, but you can still install bash on them or update existing shell version. To the point - the FreeBSD du implementation differs from that on Linux, so you may need to change du options too.
As were mentioned, you can use something like this too:
BASH:
user@host:~$ du ../ | sort -g
but it will only rightly work if you haven't sizes human readable

.. otherwise, if there are sizes with commas, etc, it will not be sorted correctly...
Good luck and save your space
