Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- POSIXLY_CORRECT=yes
- ND="0"
- NF="0"
- normalize="0"
- ignore="0"
- FILE_ERE=""
- DIR_NEW=""
- while getopts ":i:n" option;
- do
- case $option in
- i) ignore=1;
- FILE_ERE=$OPTARG;;
- n) normalize=1;;
- *) echo "Use options [-i FILE_ERE] [-n] [DIR]";;
- esac
- done
- shift $(($OPTIND - 1))
- DIR_NEW=${1#?}
- if [ -z != $DIR_NEW ] && [ -d $DIR_NEW ]; then
- DIR=$DIR_NEW
- elif [ -z != $DIR_NEW ] && [ -d != $DIR_NEW ]; then
- echo "Root directory doesn't exist!"
- exit 1
- else
- DIR="."
- fi
- size_100B="0"
- size_1KIB="0"
- size_10KIB="0"
- size_100KIB="0"
- size_1MIB="0"
- size_10MIB="0"
- size_100MIB="0"
- size_1GIB="0"
- size_OVER="0"
- print_hash ()
- {
- j="0"
- while [ $j -lt $1 ]
- do
- printf "#"
- j=$((j+1))
- done
- printf "\n"
- }
- file_size ()
- {
- size="$(wc -c <"$file")"
- if [ "$size" -lt 100 ];then
- size_100B=$((size_100B+1))
- elif [ "$size" -lt 1024 ];then
- size_1KIB=$((size_1KIB+1))
- elif [ "$size" -lt 10240 ];then
- size_10KIB=$((size_10KIB+1))
- elif [ "$size" -lt 1002400 ];then
- size_100KIB=$((size_100KIB+1))
- elif [ "$size" -lt 1048576 ];then
- size_1MIB=$((size_1MIB+1))
- elif [ "$size" -lt 10485760 ];then
- size_10MIB=$((size_10MIB+1))
- elif [ "$size" -lt 104857600 ];then
- size_100MIB=$((size_100MIB+1))
- elif [ "$size" -lt 1073741824 ];then
- size_1GIB=$((size_1GIB+1))
- else
- size_OVER=$((size_OVER+1))
- fi
- }
- FTLIST=""
- string=""
- NL=$'\n'
- LENGTH="43"
- file_type ()
- {
- short_string="$(file -b $1 | cut -c -40)"
- if [ ${#short_string} -eq 40 ]; then
- short_string="${short_string}..."
- else
- while [ ${#short_string} -lt $LENGTH ]
- do
- short_string="${short_string} "
- done
- fi
- string="${string} ${short_string}: ${NL}"
- }
- print_fthist ()
- {
- string="$(echo "$string" | sort | uniq -c | sort -n -r | cut -c 7-)"
- echo "$(echo "$string" | cut -c 2- | head -n 10)"
- }
- search ()
- {
- cd "$1"
- for file in $(ls)
- do
- if [ $ignore == 1 ] && [ $file == $FILE_ERE ]; then
- a="42"
- else
- if [ -d "$file" ] && [ -r "$file" ]; then
- search $file
- ND=$((ND+1))
- elif [ -f "$file" ] && [ -r "$file" ]; then
- file_size $file
- file_type $file
- NF=$((NF+1))
- fi
- fi
- done
- cd ..
- }
- search "$DIR"
- if [ "$DIR" != "." ]; then
- DIR=/$DIR
- fi
- echo "Root directory: $DIR"
- echo "Directories: $ND"
- echo "All files: $NF"
- echo "File size histogram:"
- printf " <100 B : "
- print_hash $size_100B
- printf " <1 KiB : "
- print_hash $size_1KIB
- printf " <10 KiB : "
- print_hash $size_10KIB
- printf " <100 KiB: "
- print_hash $size_100KIB
- printf " <1 MiB : "
- print_hash $size_1MIB
- printf " <10 MiB : "
- print_hash $size_10MIB
- printf " <100 MiB: "
- print_hash $size_100MIB
- printf " <1 GiB : "
- print_hash $size_1GIB
- printf " >=1 GiB : "
- print_hash $size_OVER
- echo "File type histogram:"
- print_fthist
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement