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.


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.
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

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!
Hide

Top Tags

Hide

Functions

Psst. Open beta.

Wow, didn't really expect you to read this far down. The latest iteration of the site is in open beta. It's a gentle open beta-- not in prime-time just yet. It's being hosted over at UpGuard (link) and you are more than welcome to give it a shot. Couple things:

  • » The open beta is running a copy of the database that will not carry over to the final version. Don't post anything you don't mind losing.
  • » If you wish to use your user account, you will probably need to reset your password.
Your feedback is appreciated via the form on the beta page. Thanks! -Jon & CLFU Team

Get the date for the last Saturday of a given month

Terminal - Get the date for the last Saturday of a given month
cal 04 2012 | awk 'NF <= 7 { print $7 }' | grep -v "^$" | tail -1
2012-05-03 16:57:45
User: javidjamae
Functions: awk cal grep tail
-2
Get the date for the last Saturday of a given month

This is a little trickier than finding the last Sunday, because you know the last Sunday is in the first position of the last line. The trick is to use the NF less than or equal to 7 so it picks up all the lines then grep out any empty lines.

Alternatives

There are 2 alternatives - vote for the best!

Terminal - Alternatives
cal 04 2012 | awk '{ $7 && X=$7 } END { print X }'
2012-05-06 23:43:21
User: flatcap
Functions: awk cal
2

If your locale has Monday as the first day of the week, like mine in the UK, change the two $7 into $6

desiredDay=6; year=2012; month=5; n=0; while [ $(date -d "$year-$((month+1))-1 - $n day" "+%u") -ne $desiredDay ]; do n=$((n+1)); done; date -d "$year-$((month+1))-1 - $n day" "+%x"
2012-05-17 12:02:30
Functions: date
0

Choosing your year and month. You only need the gnu date command and bash. desiredDay of the week is (1..7); 1 is Monday.

If you want desiredDay of week (0..6); 0 is Sunday

desiredDay=6; year=2012; month=5; n=0; while [ $(date -d "$year-$((month+1))-1 - $n day" "+%w") -ne $desiredDay ]; do n=$((n+1)); done; date -d "$year-$((month+1))-1 - $n day" "+%x"

Know a better way?

If you can do better, submit your command here.

What others think

Uh, not sure. It's May here and I get last day of the current month.

31 using

cal 04 2012 | awk '{ $7 && X=$7 } END { print X }

Comment by Habitual 224 weeks and 2 days ago

@habitual: that's what the '04' parameter is: April. Replace it with 05 for May.

Comment by flatcap 224 weeks and 2 days ago

flatcap: But what if I WANT the last day of 04?

Comment by Habitual 224 weeks and 1 day ago

I think cal now supports knowing the first day of the week depending on your _NL_TIME_WEEK_1STDAY. Does your output of `cal 05 2012` equal:

May 2012

Su Mo Tu We Th Fr Sa

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

Comment by ClashTheBunny 223 weeks ago

http://www.birkenstock.us.com

http://www.toryburchhandbags.in.net

http://www.mulberry-handbags.co.uk

http://www.poloralph-lauren.org.uk

http://www.rolexwatchesforsale.me.uk

http://www.ferragamooutletstore.net

http://www.tiffanyjewellery.org.uk

http://www.babylissflatiron.us.com

http://www.oakleysunglasses-cheap.us.com

http://www.oakley-sunglasses.me.uk

http://www.nike-mercurial.net

http://www.mulberryhandbagssale.co.uk

http://www.michaelkors-outlet.me.uk

http://www.coach-factoryoutlets.us.com

http://www.sac-louisvuittonpascher.fr

http://www.cheapoakleysunglassesss.us.com

http://www.ralphlaurenpoloshirts.org.uk

http://www.ralphlaurenoutlet.org.uk

http://www.mont-blancpens.com.co

http://www.salvatore-ferragamo.in.net

http://www.outletraybansunglasses.us.com

http://www.toryburchshoes.in.net

http://www.burberryoutletstore.in.net

http://www.ralphlauren-outletuk.co.uk

http://www.coachoutletstoreclearance.us.com

http://www.louisvuitton-handbags.me.uk

http://www.oakley-sunglasses.org.uk

http://www.truereligionjeanssale.in.net

http://www.coachoutlethandbags.us.com

http://www.ferragamo.me.uk

http://www.adidastrainers.me.uk

http://www.cheapoakley-sunglasses.us.com

http://www.iphone-cases.name

http://www.louisvuitton-handbags.org.uk

http://www.nikeairmax90.org.uk

http://www.discount-raybansunglasses.us.com

http://www.jeanstruereligion.in.net

http://www.mulberry-handbagsoutlet.org.uk

http://www.hollistershirts.org.uk

http://www.outletlululemon.in.net

http://www.omegawatches.me.uk

http://www.katespadeuk.org.uk

http://www.poloralph-lauren.us.com

http://www.ralphlaurenpolo.com.co

http://www.tiffanyandco.me.uk

http://www.sunglassesrayban.org.uk

http://www.coachoutletfactory-store.us.com

http://www.nikeblazerpas-cher.fr

http://www.ralphlauren-uk.org.uk

http://www.fitflopsoutletsale.com

http://www.tomsshoes.me.uk

http://www.outletmulberry.org.uk

http://www.niketnpas-cher.fr

http://www.toryburchoutletstore.in.net

http://www.rolexoutlet.us.com

http://www.nikeairmax90.me.uk

http://www.reebokoutletstore.us.com

http://www.celine-outlet.us.com

http://www.rolexwatchesforsale.in.net

http://www.handbagsdesigner.us.com

http://www.rayban--sunglasses.me.uk

http://www.ralphlauren-polo.in.net

http://www.beats-bydre.org.uk

http://www.ralphlauren-polo.org.uk

http://www.wholesaleoakleysunglassess.us.com

http://www.clearancefitflopssale.com

http://www.oakleysunglassess-outlet.us.com

http://www.rolexwatchesoutlet.us.com

http://www.coachoutletonline-stores.us.com

http://www.mulberrysale.co.uk

http://www.coachoutletclearance.us.com

http://www.poloralph-lauren.in.net

http://www.longchamppas-cher.fr

http://www.nikeairmax90.co.uk

http://www.beatsbydre.me.uk

http://www.tiffanyoutlet.in.net

http://www.outletchristianlouboutin.us.com

http://www.ralphlaurenpolo.us.com

http://www.katespade.org.uk

http://www.handbagschanel.us.com

http://www.burberrysaleoutlet.us.com

http://www.truereligionjeans.org.uk

http://www.swarovskicrystal.in.net

http://www.polo-ralphlauren.com.co

http://www.truereligionoutlet.org.uk

http://www.ferragamoshoes.org.uk

http://www.hollisterclothing.cn.com

http://www.handbagslongchamp.us.com

http://www.coachhandbagsclearance.us.com

http://www.michael-korsoutlet.org.uk

http://www.truereligion-sale.in.net

http://www.lululemonoutlet.org.uk

http://www.saclongchamp-pliage.fr

http://www.tiffanyand-co.in.net

http://www.katespadehandbags.co.uk

http://www.canada-oakleysunglasses.com

http://www.ferragamoshoes.com.co

http://www.chaussurelouboutin.fr

http://www.fitflopsshoes.in.net

http://www.oakleysunglassessdiscount.us.com

http://www.lululemonoutletonline.us.com

http://www.tiffanyandcos.in.net

http://www.bottega-venetaoutlet.net

http://www.longchamp-handbags.com.co

http://www.ralph-laurenoutlet.in.net

http://www.christianlouboutinuk.org.uk

http://www.toryburchoutlet-online.us.com

http://www.storecoachoutlet.us.com

http://www.hollisteruk.org.uk

http://www.nikeairforce1.fr

http://www.longchamppliage.fr

http://www.fitflopssale.in.net

http://www.truereligion.org.uk

http://www.ralphlauren-pascher.fr

http://www.replicawatches-forsale.us.com

http://www.iphone-cases.name

http://www.coachoutletonline-factory.us.com

http://www.mulberry-bags.co.uk

http://www.outletlongchamp.in.net

http://www.swarovskicrystals.com.co

http://www.wholesale-oakleysunglasses.us.com

http://www.timberlandboots.me.uk

http://www.fitflopssaleclearance.in.net

http://www.jordan-pascher.fr

http://www.oakley-sunglasseswholesale.us.com

http://www.ray-bansunglassesonline.us.com

http://www.cheapreplicawatches.net

http://www.burberry-outletstore.in.net

http://www.michael-korsfactoryoutlet.us.com

http://www.hollisterclothingstore.in.net

http://www.burberryonlineoutlet.in.net

http://www.louboutinpas-cher.fr

http://www.oakleysunglassescanada.ca

http://www.ferragamo-shoes.us.com

http://www.fitflopssale.co.uk

http://www.wholesale-nbajerseys.com

http://www.michael-korshandbags.co.uk

http://www.pololacostepascher.fr

http://www.oakleysunglassesswholesale.us.com

http://www.reeboktrainers.org.uk

http://www.outlettoryburch.in.net

http://www.michael-korsoutlet.me.uk

http://www.jewelrytiffany.in.net

http://www.truereligionjeansoutlet.co.uk

http://www.coach-onlineoutlet.us.com

http://www.outlettruereligion.in.net

http://www.tiffany-jewelry.com.co

http://www.ghdhairstraighteners.org.uk

http://www.longchamp-solde.fr

http://www.fitflopsshoes.org.uk

http://www.montblancpenss.us.com

http://www.raybansunglasses-sale.us.com

cai20160408

Comment by xiaozhengm 19 weeks and 6 days ago

Your point of view

You must be signed in to comment.