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

In place line numbering

Terminal - In place line numbering
{ rm -f file10 && nl > file10; } < file10
2010-04-08 21:08:23
User: zlemini
Functions: nl rm
3
In place line numbering

Add permanent line numbers to a file without creating a temp file.

The rm command deletes file10 while the nl command works on the open file descriptor of file10 which it outputs into a new file again named file10.

The new file10 will now be numbered in the same directory with the same file name and content as before, but it will in fact be a new file, using (ls -i) to show its inode number will prove this.

Alternatives

There are 6 alternatives - vote for the best!

Terminal - Alternatives

Know a better way?

If you can do better, submit your command here.

What others think

It should be noted that this can be dangerous. If the command fails for some reason (disk full, syntax error, Ctrl-C pressed, whatever), you'll have an empty file and no backup.

I think it's always better to use a temporary file:

nl file10 > file10.new && mv -f file10.new file10
Comment by inof 237 weeks and 1 day ago

Your comments about syntax, disk full etc are not specific to this command, they apply to most of the commands on this site.

Be advised, the 'rm' command above deletes the directory entry which is only a reference to the actual inode.

Be more specific as to why you think it is dangerous and i will glady look into improving it.

Comment by zlemini 237 weeks and 1 day ago

I agree with inof. Both versions use two inodes and just manipulate dentries to get the naming right, so neither is more efficient. But inof's version obviously can be interrupted, and the mv doesn't happen until nl has finished. Whereas with rm && nl you may interrupt, and have rm complete without nl finishing, and you are left without a file. It is true that a similar criticism applies to many of the commands at this site, but in this case by rearranging the operations, I think the "danger" is lessened. I think relying on the dentry/inode behavior of the filesystem layer is neat, but inof's version is more OS-agnostic. Would this work under Cygwin?

Comment by bwoodacre 237 weeks and 1 day ago

The issues with replacement using this method are documented at:

http://www.pixelbeat.org/docs/unix_file_replacement.html

Comment by pixelbeat 236 weeks and 5 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts