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; }

Remove current directory

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]$

-2
By: oshazard
2010-01-17 11:34:38

These Might Interest You

What Others Think

If you're doing something like this: "rm -Rf ../$blah/", you have to make sure that $blah is set, or at least give it a default value like ${blah:-DEFAULTVALUE} and quote it. You need to quote $PWD also. Imagine that $PWD is unset or has a space in it in this scenario. "basename $PWD" fails and $blah remains unset. The next command becomes "rm -Rf ..//", which could be disasterous if you're one level up from the root directory.
eightmillion · 435 weeks and 4 days ago
Thank you eightmillion. Can you find any problems with this revision? 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; }
oshazard · 435 weeks and 3 days ago
That is a bit better, although you don't need to mess with the sed business and you still need to quote your variable when you call "rm -Rf" or you could end up deleting multiple unintended directories. This is pretty workable: 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##*/}".
eightmillion · 435 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: