Skip to content

Instantly share code, notes, and snippets.

@iTrooz
Last active September 19, 2023 07:58
Show Gist options
  • Save iTrooz/30318208dff6e0ecfb27302941bdb95f to your computer and use it in GitHub Desktop.
Save iTrooz/30318208dff6e0ecfb27302941bdb95f to your computer and use it in GitHub Desktop.
initdir
# rm -rf but with safeguards, and cd into the directory
# You should put this in a sourced script (e.g .bashrc, .zshrc, etc...)
RED="\e[1;31m"
ORANGE="\e[38;5;208m"
GREEN="\e[38;5;2m"
RESET="\e[0;0m"
function initdir() {
if [ $# -eq 0 ]; then
echo "$0: Enter a folder to init"
return 1
fi
wanted_dir=`realpath "$1"`
my_dir=`realpath "$PWD"`
echo "Wanted dir : $wanted_dir"
echo "My dir : $my_dir"
if [[ "$wanted_dir" = "$my_dir" ]]; then
if ! [[ "`basename $1`" = "`basename $PWD`" ]]; then
echo -e "${RED}ERROR : To reset your current directory, you should specify it's name. Example : '$0 ../`basename $PWD`'${RESET}"
return 1
fi
echo -e "${ORANGE}Warning : resetting current directory${RESET}"
elif ! [[ "$wanted_dir" = "$my_dir"* ]]; then
echo -e "${RED}ERROR : Directory must be a child of the current directory${RESET}"
return 1
fi
echo -e "${GREEN}Resetting directory $wanted_dir${RESET}"
cd /
rm -rf $wanted_dir
mkdir -p $wanted_dir
cd $wanted_dir
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment