#!/bin/ksh
# **********************************************************************
#
# System: xxxx
#
# Filename: List_Largest_Files.sh
#
# Purpose: List 10 largest files in current partition
#
# Modification History:
# 1.0 Initial Version
# ************************************************************************
du -sk ./* | sort -rn | head | \
while read SIZE ENTRY
do
# if size > 1048576 then it is at least 1 GB big
if [ ${SIZE} -gt 1048576 ]
then
NEWSIZE=`echo "${SIZE}000 / 1048576" | bc | sed -e "s/\(...\)$/\.\1/"`
printf "% 10s %s\n" "${NEWSIZE} GB" $ENTRY
# if size > 1024 then it is at least 1 MB big
elif [ ${SIZE} -gt 1024 ]
then
NEWSIZE=`echo "${SIZE}000 / 1024" | bc | sed -e "s/\(...\)$/\.\1/"`
printf "% 10s %s\n" "${NEWSIZE} MB" $ENTRY
else
printf "% 10s %s\n" "${SIZE} KB" $ENTRY
fi
done
Excellent I used the ${ENTRY#*/} and it works not 100% how though?
It now looks like: -
du -sk ./* | sort -rn | head | \
while read SIZE ENTRY
do
# if size > 1048576 then it is at least 1 GB big
if [ ${SIZE} -gt 1048576 ]
then
NEWSIZE=`echo "${SIZE}000 / 1048576" | bc | sed -e "s/\(...\)$/\.\1/"`
printf "% 10s %s\n" "${NEWSIZE} GB" ${ENTRY#*/}
# if size > 1024 then it is at least 1 MB big
elif [ ${SIZE} -gt 1024 ]
then
NEWSIZE=`echo "${SIZE}000 / 1024" | bc | sed -e "s/\(...\)$/\.\1/"`
printf "% 10s %s\n" "${NEWSIZE} MB" ${ENTRY#*/}
else
printf "% 10s %s\n" "${SIZE} KB" ${ENTRY#*/}
fi
done
According to my colleague its the * that caused the problem. Apparently it doesn't know what directory to look in if you use du -sk * so I had to use du -sk ./*
I do not understand why you add the three 000 to the size?
Also get rid of back tics `` and use parentheses $() BC is not default installed on Ubuntu do the math in shell $((a+b)) or awk if you need decimal.
Here is some rewritten version
du -sk ./* | sort -rn | head | \
while read SIZE ENTRY
do
# if size > 1048576 then it is at least 1 GB big
if [ ${SIZE} -gt 1048576 ]
then
NEWSIZE=$((${SIZE} / 1048576))
printf "% 10s %s\n" "${NEWSIZE} GB" ${ENTRY#*/}
# if size > 1024 then it is at least 1 MB big
elif [ ${SIZE} -gt 1024 ]
then
NEWSIZE=$((${SIZE} / 1024))
printf "% 10s %s\n" "${NEWSIZE} MB" ${ENTRY#*/}
else
printf "% 10s %s\n" "${SIZE} KB" ${ENTRY#*/}
fi
done