Advertisement
Guest User

dir

a guest
Mar 24th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.79 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. POSIXLY_CORRECT=yes
  4. ND="0"
  5. NF="0"
  6. normalize="0"
  7. ignore="0"
  8. FILE_ERE=""
  9. DIR_NEW=""
  10.  
  11. while getopts ":i:n" option;
  12. do
  13.     case $option in
  14.     i) ignore=1;
  15.        FILE_ERE=$OPTARG;;
  16.     n) normalize=1;;
  17.     *) echo "Use options [-i FILE_ERE] [-n] [DIR]";;
  18.     esac
  19. done
  20.  
  21. shift $(($OPTIND - 1))
  22. DIR_NEW=${1#?}
  23. if [ -z != $DIR_NEW ] && [ -d $DIR_NEW ]; then
  24.     DIR=$DIR_NEW
  25. elif [ -z != $DIR_NEW ] && [ -d != $DIR_NEW ]; then
  26.     echo "Root directory doesn't exist!"
  27.     exit 1
  28. else
  29.     DIR="."
  30. fi
  31.  
  32. size_100B="0"
  33. size_1KIB="0"
  34. size_10KIB="0"
  35. size_100KIB="0"
  36. size_1MIB="0"
  37. size_10MIB="0"
  38. size_100MIB="0"
  39. size_1GIB="0"
  40. size_OVER="0"
  41.  
  42. print_hash ()
  43. {
  44. j="0"
  45. while [ $j -lt $1 ]
  46.     do
  47.         printf "#"
  48.         j=$((j+1))
  49.     done
  50. printf "\n"
  51. }
  52.  
  53. file_size ()
  54. {
  55. size="$(wc -c <"$file")"
  56. if [ "$size" -lt 100 ];then
  57.     size_100B=$((size_100B+1))
  58.  
  59. elif [ "$size" -lt 1024 ];then
  60.         size_1KIB=$((size_1KIB+1))
  61.  
  62. elif [ "$size" -lt 10240 ];then
  63.         size_10KIB=$((size_10KIB+1))
  64.  
  65. elif [ "$size" -lt 1002400 ];then
  66.         size_100KIB=$((size_100KIB+1))
  67.  
  68. elif [ "$size" -lt 1048576 ];then
  69.         size_1MIB=$((size_1MIB+1))
  70.  
  71. elif [ "$size" -lt 10485760 ];then
  72.         size_10MIB=$((size_10MIB+1))
  73.  
  74. elif [ "$size" -lt 104857600 ];then
  75.         size_100MIB=$((size_100MIB+1))
  76.  
  77. elif [ "$size" -lt 1073741824 ];then
  78.         size_1GIB=$((size_1GIB+1))
  79.  
  80. else
  81.     size_OVER=$((size_OVER+1))
  82. fi
  83. }
  84.  
  85. FTLIST=""
  86. string=""
  87. NL=$'\n'
  88. LENGTH="43"
  89.  
  90. file_type ()
  91. {
  92. short_string="$(file -b $1 | cut -c -40)"
  93. if [ ${#short_string} -eq 40 ]; then
  94.     short_string="${short_string}..."
  95. else
  96.     while [ ${#short_string} -lt $LENGTH ]
  97.     do
  98.         short_string="${short_string} "
  99.     done
  100. fi
  101. string="${string} ${short_string}: ${NL}"
  102. }
  103.  
  104. print_fthist ()
  105. {
  106. string="$(echo "$string" | sort | uniq -c | sort -n -r | cut -c 7-)"
  107. echo "$(echo "$string" | cut -c 2- | head -n 10)"
  108. }
  109.  
  110. search ()
  111. {
  112. cd "$1"
  113. for file in $(ls)
  114. do
  115.     if [ $ignore == 1 ] && [ $file == $FILE_ERE ]; then
  116.         a="42"
  117.     else
  118.  
  119.         if [ -d "$file" ] && [ -r "$file" ]; then
  120.             search $file
  121.             ND=$((ND+1))
  122.  
  123.         elif [ -f "$file" ] && [ -r "$file" ]; then
  124.             file_size $file
  125.             file_type $file
  126.             NF=$((NF+1))
  127.         fi
  128.     fi
  129. done
  130. cd ..
  131. }
  132.  
  133. search "$DIR"
  134.  
  135. if [ "$DIR" != "." ]; then
  136.     DIR=/$DIR
  137. fi
  138.  
  139. echo "Root directory: $DIR"
  140. echo "Directories: $ND"
  141. echo "All files: $NF"
  142. echo "File size histogram:"
  143. printf "  <100 B  : "
  144. print_hash $size_100B
  145. printf "  <1 KiB  : "
  146. print_hash $size_1KIB
  147. printf "  <10 KiB : "
  148. print_hash $size_10KIB
  149. printf "  <100 KiB: "
  150. print_hash $size_100KIB
  151. printf "  <1 MiB  : "
  152. print_hash $size_1MIB
  153. printf "  <10 MiB : "
  154. print_hash $size_10MIB
  155. printf "  <100 MiB: "
  156. print_hash $size_100MIB
  157. printf "  <1 GiB  : "
  158. print_hash $size_1GIB
  159. printf "  >=1 GiB : "
  160. print_hash $size_OVER
  161. echo "File type histogram:"
  162. print_fthist
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement