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

Protect directory from an overzealous rm -rf *

Terminal - Protect directory from an overzealous rm -rf *
cd <directory>; touch ./-i
2011-05-12 11:01:58
User: ljmhk
Functions: cd touch
15
Protect directory from an overzealous rm -rf *

Forces the -i flag on the rm command when using a wildcard delete.

Alternatives

There are 4 alternatives - vote for the best!

Terminal - Alternatives
sudo chattr -R +i dirname
2011-05-17 05:05:49
User: mamue
Functions: chattr sudo
Tags: touch
6

-R Recursively change attributes of directories and their contents.

+i to set the immutable bit to prevent even root from erasing or changing the contents of a file.

Know a better way?

If you can do better, submit your command here.

What others think

Clever.

Comment by kaedenn 167 weeks and 6 days ago

Great.

But why should you change directory? Just do the:

touch /path/-i
Comment by netizen 167 weeks and 6 days ago

I'm constantly tripping over the 'alias rm="rm -i"' that gets forced on me with Fedora/Debian Linux distros. I like this idea for a per-directory molly-guard.

Comment by Mozai 167 weeks and 6 days ago

Unfortunately it doesn't save from

rm -fr ./*

or when deleting parent dirs:

touch ./path/-i rm -fr *
Comment by aikikode 167 weeks and 6 days ago

Very interesting but doesn't seem to work here on a Mac nor a CentOS 5.3 machine using bash 3.2

david@host /tmp $

cd a

david@host /tmp/a $

mkdir ta tb tc

david@host /tmp/a $

touch ta/-i

david@host /tmp/a $

rm -rf *

david@host /tmp/a $

ls

david@host /tmp/a $

Comment by DaveQB 167 weeks and 6 days ago

DaveQB ... did you read aikikode's comment?

Works fine for me on Mac OSX.

Comment by unixmonkey21519 167 weeks and 6 days ago

My vote is up, but still it will not protect against rm -rf -- *

Nice thinking though.

Comment by RanyAlbeg 167 weeks and 6 days ago

safe-rm (http://www.safe-rm.org.nz) is another way of preventing accidental deletions through a blacklist of paths you don't want to delete (e.g. /bin, /usr/lib or ~/important_file.gpg)

Comment by fmarier 167 weeks and 5 days ago

fmarier, that looks interesting I'll take a look. oh and thanks for the up votes guys, its not perfect but handy when training the junior sys admins.

Comment by ljmhk 167 weeks and 5 days ago

If you really want to protect files from being deleted or change, even by root,

use the chattr command:

chattr +i

http://en.wikipedia.org/wiki/Chattr

Only works on ext2 systems.

Comment by laebshade 167 weeks and 5 days ago

@unixmonkey21519 aikikode Must have posted seconds before me as I did not see it just before or I posted (or even after actually)

Comment by DaveQB 167 weeks and 3 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts