|
| 1 | +#!/bin/zsh |
| 2 | +# Copies the contents of the current directory into the parent |
| 3 | +# directory and then deletes the current directory |
| 4 | +unwrap() { |
| 5 | + local this_dir=`basename $PWD`; |
| 6 | + local sys_dirs=`ls /`; |
| 7 | + local in_sys_dir=`echo ${sys_dirs[@]} | grep -o $this_dir | wc -w`; |
| 8 | + local user_protected_dirs=`cat ~/.unwrap` |
| 9 | + local in_user_protected_dirs=`echo ${user_protected_dirs[@]} | grep -o $this_dir | wc -w`; |
| 10 | + |
| 11 | + # Check for dangerous directories |
| 12 | + if [[ $this_dir == '/' ]]; then |
| 13 | + echo "\033[38;5;124mRoot directory detected. Aborting."; |
| 14 | + return 0; |
| 15 | + elif [[ in_sys_dir -gt 0 ]]; then |
| 16 | + echo "\033[38;5;124mSystem directory detected. Aborting."; |
| 17 | + return 0; |
| 18 | + elif [[ in_user_protected_dirs -gt 0 ]]; then |
| 19 | + echo "\033[38;5;124mUser protected directory detected. Aborting."; |
| 20 | + return 0; |
| 21 | + elif [[ $this_dir == `basename $HOME` ]]; then |
| 22 | + echo "\033[38;5;124mHome directory detected. Aborting."; |
| 23 | + return 0; |
| 24 | + else |
| 25 | + # The usual suspect have been eliminated. Proceed with a warning. |
| 26 | + echo "\n\033[38;5;124mWarning:\n" |
| 27 | + echo "\033[38;5;024mThis will move all files into the parent directory" |
| 28 | + echo "and then delete this folder.\n" |
| 29 | + echo "\033[38;5;124mExisting files will be overwritten.\n" |
| 30 | + |
| 31 | + echo -n "\033[38;5;024mDo you wish to proceed? (y/N)\n\033[0m"; |
| 32 | + |
| 33 | + read response; |
| 34 | + |
| 35 | + # Check to see if the directory is empty |
| 36 | + if [[ $response == "y" ]]; then |
| 37 | + if [ -z "$(ls -A)" ]; then |
| 38 | + # Empty directory. Move into parent directory and delete |
| 39 | + echo "\n⚙️ \033[38;5;144m No files to move. Proceeding.\033[0m"; |
| 40 | + else |
| 41 | + echo "\n⚙️ \033[38;5;144m Moving files" |
| 42 | + cp -r * ../ |
| 43 | + fi |
| 44 | + echo "\n✅ \033[38;5;118mFiles moved, removing directory" |
| 45 | + cd .. |
| 46 | + rm -rf $this_dir |
| 47 | + fi |
| 48 | + return 1 |
| 49 | + fi |
| 50 | +} |
0 commit comments