Instructions to set up a basic LAMP+SSH server environment
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.8 KiB

6 years ago
  1. ###########################################################
  2. # List files and directories which are not owned by any package in a Debian system
  3. # Scripted by Fincer (~Pekka Helenius), 2017
  4. echo -e "\nSearch for files & folders which are not owned by any installed package.\n"
  5. if [[ $# -eq 0 ]]; then
  6. read -r -p "Folder path: " BASEDIR
  7. #Substitute $ symbol from environmental variables for printenv input
  8. if [[ $BASEDIR == *"$"* ]]; then
  9. BASEDIR=$(echo $(printenv $(echo ${BASEDIR} | sed 's/\$//g')))
  10. fi
  11. else
  12. BASEDIR=$1
  13. fi
  14. if [[ ! $(file --mime-type "${BASEDIR}" | grep "inode/directory" | wc -l) -eq 1 ]]; then
  15. echo "ERROR: Use full folder path as an input value!"
  16. elif [[ $# -gt 1 ]]; then
  17. echo "ERROR: Only one argument accepted!"
  18. else
  19. echo -e "Search depth:\n1 = "${BASEDIR}"\n2 = "${BASEDIR}" & subfolders\n3 = "${BASEDIR}", subfolders & 2 folder levels below\n4 = no limit\n"
  20. read -r -p "Which depth value you prefer? [Default: 1] " response
  21. case $response in
  22. 1)
  23. depth="-maxdepth 1 "
  24. depthstr="${BASEDIR}"
  25. ;;
  26. 2)
  27. depth="-maxdepth 2 "
  28. depthstr="${BASEDIR} and subfolders"
  29. ;;
  30. 3)
  31. depth="-maxdepth 3 "
  32. depthstr="${BASEDIR}, subfolders and 2 folder levels below"
  33. ;;
  34. 4)
  35. depth=""
  36. depthstr="${BASEDIR} and all subfolders"
  37. ;;
  38. *)
  39. echo -e "\nUsing default value [1]"
  40. depth="-maxdepth 1 "
  41. depthstr="${BASEDIR}"
  42. esac
  43. echo -e "\nSearching unowned files in $depthstr\n"
  44. function counter() {
  45. i=0
  46. n=1
  47. COUNT=$(echo "$DATASET" | wc -l)
  48. IFS=$'\n'
  49. for data in $DATASET; do
  50. echo -ne "Scanning $data_name $n ($(( 100*$n/$COUNT ))%) of all $type ($COUNT) in $depthstr\r"
  51. if [[ $(dpkg -S "${data}" &>/dev/null || echo "no path found matching pattern" | wc -l) -eq 1 ]]; then
  52. DATA_ARRAY[$i]="$(( $i + 1)) - ${data}"
  53. let i++
  54. fi
  55. let n++
  56. done
  57. unset IFS
  58. if [[ $i -gt 0 ]]; then
  59. echo -e "\nThe following $i of $COUNT $type is not owned by any installed package in $depthstr:\n"
  60. IFS=$'\n'
  61. echo -e "${DATA_ARRAY[*]}\n"
  62. unset IFS
  63. unset DATA_ARRAY
  64. else
  65. echo -e "\nAll $type are owned by system packages in $depthstr.\n"
  66. fi
  67. }
  68. function files() {
  69. DATASET=$(find "${BASEDIR}" ${depth} -type f)
  70. type="files"
  71. data_name="file"
  72. counter
  73. }
  74. function folders() {
  75. DATASET=$(find "${BASEDIR}" ${depth} -type d)
  76. type="folders"
  77. data_name="folder"
  78. counter
  79. }
  80. files; folders
  81. fi