Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Remove current directory

Terminal - 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; }
2010-01-17 11:34:38
User: oshazard
Functions: basename cd echo read rm sed
-3
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

Alternatives

There are 11 alternatives - vote for the best!

Terminal - Alternatives

Know a better way?

If you can do better, submit your command here.

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.

Comment by eightmillion 244 weeks 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; }
Comment by oshazard 244 weeks 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##*/}".

Comment by eightmillion 243 weeks and 6 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts