-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunwrap.plugin.zsh
66 lines (61 loc) · 2.4 KB
/
unwrap.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/zsh
# Copies the contents of the current directory into the parent
# directory and then deletes the current directory
unwrap() {
local this_dir=`basename $PWD`;
local sys_dirs=`command ls /`;
local in_sys_dir=`echo ${sys_dirs[@]} | grep -o $this_dir | wc -w`;
if [ -e "$HOME/.unwrap" ]; then
local user_protected_dirs=`cat $HOME/.unwrap`;
local in_user_protected_dirs=`echo ${user_protected_dirs[@]} | grep -o $this_dir | wc -w`;
else;
local in_user_protected_dirs=0;
fi;
# Check for dangerous directories
if [[ $this_dir == '/' ]]; then
echo "\033[38;5;124mRoot directory detected. Aborting.";
return 0;
elif [[ in_sys_dir -gt 0 ]]; then
echo "\033[38;5;124mSystem directory detected. Aborting.";
return 0;
elif [[ in_user_protected_dirs -gt 0 ]]; then
echo "\033[38;5;124mUser protected directory detected. Aborting.";
return 0;
elif [[ $this_dir == `basename $HOME` ]]; then
echo "\033[38;5;124mHome directory detected. Aborting.";
return 0;
else
# The usual suspect have been eliminated. Proceed with a warning.
echo "\n\033[38;5;124mWarning:\n"
echo "\033[38;5;024mThis will move all files into the parent directory"
echo "and then delete this folder.\n"
echo "\033[38;5;124mExisting files will be overwritten.\n"
echo -n "\033[38;5;024mDo you wish to proceed? (y/N)\n\033[0m";
read response;
# Check to see if the directory is empty
if [[ $response == "y" ]]; then
if [ -z "$(ls -A)" ]; then
# Empty directory. Move into parent directory and delete
echo "\n⚙️ \033[38;5;144m No files to move. Proceeding.\033[0m";
else
echo "\n⚙️ \033[38;5;144m Moving files"
cp -r * ../
fi
echo "\n✅ \033[38;5;118mFiles moved, removing directory"
cd ..
if rmdir -v "$this_dir" 2>/dev/null; then
echo "\n✅ \033[38;5;118mEmpty directory removed. Process completed."
else
echo "\n⚠️ \033[38;5;214mDirectory not empty, Something went wrong.\n"
echo -n "\033[38;5;024mDo you wish to force delete the folder including the files? (y/N)\n\033[0m";
read check;
if [[ $check == "y" ]]; then
echo "\033[38;5;124mRemoving directory forcefully."
command rm -rf "$this_dir"
fi
echo "\n \033[38;5;118mDirectory not removed. Process aborted."
fi
fi
return 1
fi
}