Advertisement
nanorocks

file_organizer_shell

Jan 14th, 2018
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.78 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -euo pipefail
  4. #set -euxo pipefail
  5. #
  6. # CREATOR
  7. # This code in writen by nanorocks
  8. #
  9.  
  10. #
  11. # POINT
  12. # Helping script to organize your files in folders
  13. #
  14.  
  15. #
  16. # USAGE
  17. # For OS's who compile SHELL SCRIPTS
  18. #
  19.  
  20. #GLOBAL VARIABLES
  21. full_path=0
  22. tmp_path=0
  23.  
  24. function menu_print() # printing menu with echo 1,2,3,4 /DONE
  25. {
  26. echo -e "\n  MENU                                                 "
  27. echo "  1) USAGE                                                  "
  28. echo "  2) Organize specific type BY: < .NameExtension >          "
  29. echo "  3) Find specific type BY: < .NameExtension >              "
  30. echo "  4) EXIT                                                   "
  31. echo "                                                            "
  32. }
  33.  
  34. function logo() # printing logo with echo and important things /DONE
  35. {
  36. echo "+++++++++++++++++++++++++++++++++++++++++++++"
  37. echo "+  _       _                     _          +"
  38. echo "+ |_o| _  / \.__  _.._ o_  _ ._ |_ |_  _ || +"
  39. echo "+ | ||(/_ \_/|(_|(_|| ||/_(/_|   _|| |(/_|| +"
  40. echo "+              _|                           +"
  41. echo "+++++++++++++++++++++++++++++++++++++++++++++"
  42.  
  43. echo -e "\nIMPORTANT: Before using this shell script, read the 1) USAGE section of the menu below."
  44.  
  45. }
  46.  
  47. function rec() { # recursion starting on begining to rename files with spaces in their title  /DONE
  48.  rename 's/ /_/g' *
  49.  for i in `ls $tmp_path`
  50.  do
  51.     #echo $i, `file $i`
  52.     if [ -d $i ]
  53.     then
  54.         # echo "Directorium: $i"
  55.         cd $i
  56.         rec .
  57.         cd ..
  58.     fi
  59.  done
  60. }
  61.  
  62. function validation_start() # important thing for PATH validation /DONE
  63. {
  64. echo -e "-> Enter your path to folder: "
  65. read path
  66.  
  67. # if ! [ ${path:0:1} = "/" ]  
  68. # then
  69.     # slas="/"
  70.     # path=$slas$path
  71.     # echo "You path must start with '/home'"
  72. # fi
  73.  
  74. if ! [ ${path:0:5} = "/home" ] # only works on /home
  75. then
  76.     echo -e "You path must start with '/home'"
  77.     exit 0
  78. fi
  79.  
  80. while ! [[ -d "$path" ]];   # validation if path is valid dir
  81. do
  82.     echo -e "-> Invalid path! Try again or ctrl+c/ctrl+z to EXIT."
  83.     echo -e "-> Enter your path to folder: "
  84.     read path
  85.     # exit 0
  86. done
  87.  
  88. echo -e "-> Valid path!"
  89. cd $path
  90. tmp_path=$path
  91. rec .
  92. }
  93.  
  94. function menu_selection_validation() # validation on menu -> numeric valid number or text /DONE
  95. {
  96. menu_print .
  97. echo "-> Select number: "
  98. read num
  99.  
  100. re='^[0-9]+$'
  101.  
  102. while ! [[ $num =~ $re ]] ; do #fix bugs here DONE
  103.    echo "-> Not a number. Enter valid number.";
  104.    read num
  105. done
  106.  
  107. while [[ $num -gt 4 ]] ; do  #fix bugs here DONE
  108.    echo "-> Not a valid number. Try again.";
  109.    read num
  110. done
  111.  
  112. }
  113.  
  114. function validation_yes_no() # validation for yes/no answers /DONE
  115. {
  116.  
  117. read three_tmp
  118.        
  119. while [ "$three_tmp" != "y" ] && [ "$three_tmp" != "n" ];
  120. do
  121.     echo "Invalid input."
  122.     echo -e "$*"
  123.     read three_tmp
  124. done
  125. }
  126.  
  127. function main()
  128. {
  129.  
  130. if [ $num = "4" ] # terminating shell script /DONE
  131. then    
  132.     echo "-> Terminated. Bye. "
  133.     exit 0
  134. fi
  135.  
  136. if [ $num = "3" ] # find specific type BY NameExtension /DONE
  137. then
  138.     if [ $full_path = 0 ]
  139.     then
  140.         full_path=1
  141.         validation_start .
  142.     fi
  143.    
  144.     echo -e "\n-> Enter file type: "
  145.     read format_files
  146.    
  147.     if [[ $format_files != "."* ]]; then # validation format --> /DONE
  148.         format_files="."$format_files
  149.     fi
  150.    
  151.     tmp=`find $path -name "*$format_files"`
  152.     size=${#tmp}
  153.  
  154.     str="-> Do you want to find NEW specific type (y/n)?"
  155.    
  156.     while [[ "$size" = 0 ]]
  157.     do
  158.         echo "-> File type not exist in: "`pwd`
  159.         echo "$str"
  160.          
  161.         validation_yes_no $str # validation format if NOT begins with "." add one e.g mp3 -> .mp3 |--> /DONE
  162.        
  163.         if [ $three_tmp = "y" ]
  164.         then
  165.             main .
  166.             exit 0
  167.         elif [ $three_tmp = "n" ]
  168.         then
  169.             echo "-> Redirect to MENU."
  170.             sleep 0.5
  171.             menu_selection_validation .
  172.             main .
  173.             exit 0
  174.         fi
  175.     done
  176.    
  177.     echo -e "$tmp\n"
  178.     echo "$str"
  179.    
  180.     validation_yes_no $str  # add here validation --> DONE
  181.    
  182.     if [ $three_tmp = "y" ]
  183.     then
  184.         main .
  185.         exit 0
  186.     elif [ $three_tmp = "n" ]
  187.     then
  188.         echo "-> Redirect to MENU."
  189.         sleep 0.5
  190.         menu_selection_validation .
  191.         main .
  192.         exit 0
  193.     fi
  194.    
  195. fi
  196.  
  197. if [ $num = "2" ] # organize specific type BY NameExtension /DONE
  198. then
  199.     if [ $full_path = 0 ]
  200.     then
  201.         full_path=1
  202.         validation_start .
  203.     fi
  204.     echo -e "\n-> Enter file type: " # add here validation
  205.     read format_files
  206.    
  207.     if [[ $format_files != "."* ]]; then # validation format if NOT begins with "." add one e.g mp3 -> .mp3 |--> /DONE
  208.         format_files="."$format_files
  209.     fi
  210.    
  211.     tmp=`find $path -name "*$format_files"`
  212.     size=${#tmp}
  213.    
  214.     while [[ "$size" = 0 ]]
  215.     do
  216.         echo "-> File type not exist in: "`pwd`
  217.         str="-> Do you want to organize NEW specific type (y/n)?"
  218.         echo "$str" # add here validation ---> DONE
  219.        
  220.         validation_yes_no $str
  221.        
  222.         if [ $three_tmp = "y" ]
  223.         then
  224.             main .
  225.             exit 0
  226.         elif [ $three_tmp = "n" ]
  227.         then
  228.             echo "-> Redirect to MENU."
  229.             sleep 1
  230.             menu_selection_validation .
  231.             main .
  232.             exit 0
  233.         fi
  234.     done
  235.    
  236.     if [ "$size" -gt 0 ]
  237.     then
  238.         echo -e "\n-> Enter folder name:" # add here validation
  239.         read fname
  240.        
  241.         while [ -d $path$fname ]
  242.         do
  243.             echo "-> Folder exist. Enter NEW folder name:"
  244.             read fname
  245.         done
  246.        
  247.         mkdir $fname
  248.        
  249.         for i in $tmp
  250.         do
  251.             echo "-> MOVING: $i to $fname"
  252.             mv $i $fname
  253.             sleep 0.2
  254.         done
  255.     fi
  256.    
  257.     str=" \n-> Do you want to organize NEW specific type (y/n)?"
  258.     echo -e "$str" # add here validation ---> DONE
  259.    
  260.     validation_yes_no $str
  261.    
  262.     if [ $three_tmp = "y" ]
  263.     then
  264.         main .
  265.         exit 0
  266.     elif [ $three_tmp = "n" ]
  267.     then
  268.         echo "-> Redirecting to MENU."
  269.         sleep 0.5
  270.         menu_selection_validation .
  271.         main .
  272.         exit 0
  273.     fi
  274. fi
  275.  
  276. if [ $num = "1" ] # usage for this shell script IMPORTANT andn RECOMENDATION things
  277. then
  278.     echo -e "\n--->IMPORTANT<--- \n\nTHIS SHELL SCRIPT WON'T WORK PROPERLY IF:"
  279.     echo "-> Your files/folders have blank spaces in their name."
  280.     echo "-> Your existing folders in your inserted path begin with special characters like (?/'][{\!@#...)."
  281.     echo "-> The name of the folder that you created contains special characters at the beginning like (?/'][{\!@#...)."
  282.     echo "-> If you repeatedly enter invalid values."
  283.     echo "-> If you do not have good knowledge of scripts, do not modify the code."
  284.    
  285.     echo -e "\nRECOMENDATION:"
  286.     echo "-> If you do not satisfy any of the requirements above, this shell script will not work well. So arrange them before turning it on to work."
  287.     echo "-> If you need to use special characters when creating a folder, it is desirable to use - and _ for your folder name."
  288.     echo -e "-> If you get stuck somewhere, contact me on: andrejnankov@gmail.com \n"
  289.    
  290.     str="Do you want to be redirect to MENU (y/n)?"
  291.     echo "$str"
  292.    
  293.     validation_yes_no $str  # add here validation ---> DONE
  294.        
  295.     if [ $three_tmp = "y" ]
  296.     then
  297.         echo "-> Redirecting ..."
  298.         logo .
  299.         sleep 0.5
  300.         menu_selection_validation .
  301.         main .
  302.         exit 0
  303.        
  304.     elif [ $three_tmp = "n" ]
  305.     then
  306.         echo "-> Terminated. Bye. "
  307.         exit 0
  308.     fi
  309.    
  310.     exit 0
  311. fi
  312. }
  313.  
  314. logo .
  315. menu_selection_validation .
  316. main .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement