Remove current directory
removedir () { echo "Deleting the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=$(echo "$PWD" | sed 's/ /\\ /g'); foo=$(basename "$blah"); rm -Rf ../$foo/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }
CHANGELOG
Version 1.1
removedir () { echo "You are about to delete the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=$(echo "$PWD" | sed 's/ /\\ /g'); foo=$(basename "$blah"); rm -Rf ../$foo/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }
BUG FIX:
Folders with spaces
Version 1.0
removedir () { echo "You are about to delete the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=`basename $PWD`; rm -Rf ../$blah/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }
BUG FIX:
Hidden directories (.dotdirectory)
Version 0.9
rmdir () { echo "You are about to delete the current directory $PWD. Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=`basename $PWD`; rm -Rf ../$blah/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }
Removes current directory with recursive and force flags plus basic human check. When prompted type yes
1. [user@host ~]$ ls
foo bar
2. [user@host ~]$ cd foo
3. [user@host foo]$ removedir
4. yes
5. rm -Rf foo/
6. [user@host ~]$
7. [user@host ~]$ ls
bar
Sample Output
#### Use example ####
[user@host Downloads]$ wtzc http://raoulito.info/plugins/pidgin_screenshot/pidgin-sendscreenshot-0.6-3.tar.gz
[user@host pidgin-sendscreenshot-0.6-3]$ ls
aclocal.m4 config.h.in INSTALL m4 README
AUTHORS config.sub install-sh Makefile.am src
build-aux configure intltool-extract.in Makefile.in VERSION
ChangeLog configure.ac intltool-merge.in missing
config COPYING intltool-update.in NEWS
config.guess depcomp ltmain.sh po
[user@host pidgin-sendscreenshot-0.6-3]$ removedir
You are about to delete the current directory /home/user/Downloads/pidgin-sendscreenshot-0.6-3/ Are you sure?
yes
[user@host Downloads]$
removedir () { echo "You are about to delete the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=$(echo "$PWD" | sed 's/ /\\ /g'); foo=$(basename "$blah"); rm -Rf ../$foo/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }
removedir(){ read -p "You are about to delete the current directory $PWD Are you sure? " human;if [ "$human" = "yes" ]; then foo=$(basename "$PWD");[ -z "$foo" ] && { echo "Error: Couldn't get working directory" >&2;return 1;}; rm -Rf ../"$foo"/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }
Notice the "[ -z $foo ]" part. That checks if the variable $foo is null. If it is null, then the function echoes an error message to stderr and returns a non-zero exit status. Otherwise, it continues. Also notice "read -p". You can specify a prompt for the read command. On a side note, a neat way to get the basename of the current working directory in Bash is to use "${PWD##*/}".