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

Commands tagged comment from sorted by
Terminal - Commands tagged comment - 14 results
: "comment in line"
egrep -v "^\s*(#|$)" myfile.cfg
2011-12-08 15:25:17
User: dan
Functions: egrep
Tags: egrep comment
2

Does not print any line that either:

- is empty

- contains only spaces or tabs

- starts with #

- starts with spaces/tabs followed by a #

nocomments () { cat $1 | egrep -v '^[[:space:]]*#|^[[:space:]]*$|^[[:space:]]*;' | sed '/<!--.*-->/d' | sed '/<!--/,/-->/d'; }
2011-11-04 12:47:39
User: RuizTapiador
Functions: cat egrep sed
Tags: sed grep comment
1

Hide comments and empty lines, included XML comments,

<ESC> #
2011-09-13 17:01:44
User: ioggstream
6

This should work with different locales. Another post reports

perl -ne 'print if m{\Q/*\E}x .. m{\Q*/\E}x or m{\/\/}x' *.c
2011-07-12 04:41:49
User: dbr
Functions: perl
0

more idiomatic version of the same, using the flip-flop-operator; also printing lines with '//'-style comments

perl -e 'my $in_comment = 0; while (<>) { $in_comment = 1 if m{\Q/*\E}; print if $in_comment; $in_comment = 0 if m{\Q*/\E}; }' *.cpp
2011-07-08 00:17:27
User: doherty
Functions: perl
1

This is a naive way of finding source code comments in source code files that use C-like comments: // and /*...*/

sed 's/^#\(.*DEBUG\)/\1/' $FILE
awk '{printf("/* %02d */ %s\n", NR,$0)}' inputfile > outputfile
2011-01-04 19:13:55
User: lucasrangit
Functions: awk
1

I often find the need to number enumerations and other lists when programming. With this command, create a new file called 'inputfile' with the text you want to number. Paste the contents of 'outputfile' back into your source file and fix the tabbing if necessary. You can also change this to output hex numbering by changing the "%02d" to "%02x". If you need to start at 0 replace "NR" with "NR-1". I adapted this from http://osxdaily.com/2010/05/20/easily-add-line-numbers-to-a-text-file/.

sed -i -e 's/^#$//g' /path/to/file
grep -v ^# file.conf | grep -v ^$ > new_file.conf
sed -i '19375 s/^/#/' file
2009-10-07 17:50:40
User: TuxOtaku
Functions: sed
5

This will comment out a line, specified by line number, in a given file.

some_very_long_and_complex_command # label
2009-09-08 05:58:27
User: jamolkhon
70

When using reverse-i-search you have to type some part of the command that you want to retrieve. However, if the command is very complex it might be difficult to recall the parts that will uniquely identify this command. Using the above trick it's possible to label your commands and access them easily by pressing ^R and typing the label (should be short and descriptive).

UPDATE:

One might suggest using aliases. But in that case it would be difficult to change some parts of the command (such as options, file/directory names, etc).

sed '/^#.*DEBUG.*/ s/^#//' $FILE
if [ "${vote}" = "down" ]; then echo leave comment; fi
2009-03-20 00:31:13
Functions: echo
-10

I think it would be wise if anyone voting down left a comment indicating the reason for that action. Don't keep it to yourself. Thanks.