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.

Universal configuration monitoring and system of record for IT.

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:



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.

Top Tags



Print only the odd lines of a file

Terminal - Print only the odd lines of a file
awk '{if (NR % 2 == 1) print $0}' file.txt
2009-02-05 18:43:19
User: twopoint718
Functions: awk
Print only the odd lines of a file


There is 1 alternative - vote for the best!

Terminal - Alternatives
awk 'NR%3==1' file
2010-01-08 18:52:24
Functions: awk

A better way to show the file lines 3n + 1

sed -n '1,${p;n;n;}' foo > foo_every3_position1; sed -n '2,${p;n;n;}' foo > foo_every3_position2; sed -n '3,${p;n;n;}' foo > foo_every3_position3
2010-01-08 04:19:59
User: oshazard
Functions: sed

sed extract every nth line.

Generic is:

sed -n 'STARTPOSITION,${p;n;*LINE}' foo

where n;*LINE = how many lines. thus p;n;n; is "for every 3 lines" and p;n;n;n;n; is "for every 5 lines"

awk 'NR%2'
2011-02-25 14:28:39
User: God
Functions: awk

NR%2 == 1 on odd lines and 0 on even lines

sed 2~2d
2011-02-28 17:41:59
User: rbossy
Functions: sed

The tilde address operator is an extension of GNU sed. It won't work with POSIX sed.

awk '{if (NR % 3 == 1) print $0}' foo > foo_every3_position1; awk '{if (NR % 3 == 2) print $0}' foo > foo_every3_position2; awk '{if (NR % 3 == 0) print $0}' foo > foo_every3_position3
2010-01-08 04:20:06
User: oshazard
Functions: awk

awk extract every nth line.

Generic is:

awk '{if (NR % LINE == POSITION) print $0}' foo

where "last" position is always 0 (zero).

Know a better way?

If you can do better, submit your command here.

What others think

Downvoted for useless use of cat. Use "command

Comment by Rhomboid 323 weeks and 1 day ago

And now I see that this comment form doesn't allow braces, mangling the comment.

Comment by Rhomboid 323 weeks and 1 day ago

@Rhomboid - try prefixing your command with $.

awk '{if (NR % 2 == 1) print $0}' file.txt
Comment by DEinspanjer 323 weeks and 1 day ago

Fixed. Thanks.

Comment by twopoint718 323 weeks and 1 day ago

Odd: awk 'NR % 2 == 1' file.txt

Even: awk 'NR % 2 == 0' file.txt

Comment by systemj 323 weeks and 1 day ago

Odd: awk NR%2 file.txt

Even: awk 1-NR%2 file.txt

Comment by unbibium 323 weeks and 1 day ago

Another way:

awk '{print;getline}' file.text # ODD awk 'getline{print}' file.text # EVEN
Comment by porges 323 weeks ago

Your point of view

You must be signed in to comment.