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.

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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

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 are 5 alternatives - 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 475 weeks and 4 days ago

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

Comment by Rhomboid 475 weeks and 4 days ago

@Rhomboid - try prefixing your command with $.

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

Fixed. Thanks.

Comment by twopoint718 475 weeks and 4 days ago

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

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

Comment by systemj 475 weeks and 4 days ago

Odd: awk NR%2 file.txt

Even: awk 1-NR%2 file.txt

Comment by unbibium 475 weeks and 4 days ago

Another way:

awk '{print;getline}' file.text # ODD awk 'getline{print}' file.text # EVEN
Comment by porges 475 weeks and 3 days ago

Your point of view

You must be signed in to comment.