Skip to content

Instantly share code, notes, and snippets.

@iTrooz
Last active February 13, 2025 19:44
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...)
# Link: https://gist.github.com/iTrooz/30318208dff6e0ecfb27302941bdb95f/edit
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
# This is the current directory. Verify the user has explicitely typed the directory name to reset
if [[ "$(basename $1)" = "$(basename $PWD)" ]]; then
echo -e "${ORANGE}Warning : resetting current directory${RESET}"
else
echo -e "${RED}ERROR : To reset your current directory, you should specify it's name. Example : 'initdir ../$(basename $PWD)'${RESET}"
return 1
fi
# Check it the wanted_directory starts with the current directory
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