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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.
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

Efficient count files in directory (no recursion)

Terminal - Efficient count files in directory (no recursion)
perl -e 'if(opendir D,"."){@a=readdir D;print $#a-1,"\n"}'
2009-07-23 20:14:33
User: recursiverse
Functions: perl
1
Efficient count files in directory (no recursion)
time perl -e 'if(opendir D,"."){@a=readdir D;print $#a - 1,"\n"}'

205413

real 0m0.497s

user 0m0.220s

sys 0m0.268s

time { ls |wc -l; }

205413

real 0m3.776s

user 0m3.340s

sys 0m0.424s

*********

** EDIT: turns out this perl liner is mostly masturbation. this is slightly faster:

find . -maxdepth 1 | wc -l

sh-3.2$ time { find . -maxdepth 1|wc -l; }

205414

real 0m0.456s

user 0m0.116s

sys 0m0.328s

** EDIT: now a slightly faster perl version

perl -e 'if(opendir D,"."){++$c foreach readdir D}print $c-1,"\n"'

sh-3.2$ time perl -e 'if(opendir D,"."){++$c foreach readdir D}print $c-1,"\n"'

205414

real 0m0.415s

user 0m0.176s

sys 0m0.232s

Alternatives

There is 1 alternative - vote for the best!

Terminal - Alternatives

Know a better way?

If you can do better, submit your command here.

What others think

what if you have to recurse? I don't know how to extend the perl to recurse, but it would be interesting to compare to

find . -type f | wc -l

On the speed difference, I'm guessing perl has an advantage since it really doesn't deal with the filename strings like ls must. Somehow ls|wc seems to use twice as much system call time as the perl but also spends more time in userspace presumably shuttling filenames around. Any ideas?

Comment by bwoodacre 426 weeks ago

And if it is the filename overhead, I wonder how much faster 'ls -i | wc -l' is.

Comment by bwoodacre 426 weeks ago

In my home, I got the following counts:

$ find . -type f | wc -l 15812 $ perl -e 'if(opendir D,"."){@a=readdir D;print $#a-1,"\n"}' 109 $ ls |wc -l 14
Comment by kzh 426 weeks ago

@kzh you forgot to time the results, that is what is being measured! Also, your home dir is a bad place to test because there are many hidden files/directories which find/ls ignores as well as many levels of subdirs which perl or ls do not consider, that is why your file counts vary.

Comment by bwoodacre 426 weeks ago

~$ time perl -e 'if(opendir D,"."){@a=readdir D;print $#a - 1,"\n"}'280

real 0m0.117s

user 0m0.010s

sys 0m0.000s

~$ time { ls |wc -l; }

108

real 0m0.007s

user 0m0.010s

sys 0m0.000s

Looks like your perl line does it ~15 times slower than the ls on my machine.

Comment by tedkozma 425 weeks and 6 days ago

@bwoodacre

you could do...

perl -e 'use File::Find;$c=0;find sub {++$c;},".";print $c,"\n"'

but it is not faster than

find . | wc -l

which made me realize that

find . -maxdepth 1 | wc -l

...is faster than my original oneliner :}

i'm going to make an edit with this comment

Comment by recursiverse 425 weeks and 6 days ago

@tedkozma

the perl liner should be faster on larger directories. my test directory had ~200k files.

Comment by recursiverse 425 weeks and 6 days ago

a slight modification brings perl slightly ahead again (though not by much and only for very large directories)...

perl -e 'if(opendir D,"."){++$c foreach readdir D}print $c,"\n"'
Comment by recursiverse 425 weeks and 6 days ago
ls -U1 | wc -l

is much faster because it doesn't have to read in the whole directory listing into memory and sort it

Comment by hfs 377 weeks ago

http://www.michaelkors-handbags.in.net

http://www.canadagoosejacketssale.us.com

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

http://www.michaelkorsoutletofficials.us.com

http://www.christian-louboutinoutlet.us.org

http://www.christianlouboutin.com.co

http://www.ralphlaurenpoloshirts.me.uk

http://www.lacosteoutlet.us.org

http://www.adidasshoes.us.org

http://www.coachfactoryoutletonlinestores.us.com

http://www.ralph-laurenpolooutlet.us.com

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

http://www.michaelkorsoutletclearancee.us.com

http://www.fitflopsaleclearance.us.com

http://www.polooutlet.name

http://www.coachfactoryoutletofficials.us.com

http://www.raybansunglassesformen.us.com

http://www.mbtshoesoutlets.us.com

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

http://www.nikeshoes.com.co

http://www.cheapuggboots.name

http://www.cheapuggboots.org

http://www.raybansun-glasses.com.co

http://www.fredperry.org.uk

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

http://www.valentinooutlet.in.net

http://www.polooutletralphlauren.us.com

http://www.oakleysunglasseswholesaler.us.com

http://www.montblancpensoutlet.com

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

http://www.yeezyboost350-v2.us.com

http://www.mcmoutletonline.us

http://raybansunglasses.co-sunglasses.com

http://nikeshoes.nike-com.com

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

http://www.truereligionjeanss.us.com

http://www.pandoraoutlets.us.com

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

http://www.lunetterayban.fr

http://www.airjordan4.us

http://www.canadagooseoutlet-store.com.co

http://www.adidasnmd-r1.us.com

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

http://www.mlbjerseys.net

http://www.adidasyeezyshoes.name

http://www.poloralphlaurenfactorystore.us.com

http://www.cheapjordans.name

http://www.edhardyclothing.us.com

http://www.michaelkorsoutletclearancehandbags.us.com

http://www.polo-ralphlaurenuk.me.uk

http://www.fitflops-saleclearance.us

http://www.cheapmlb-jerseys.us

http://www.nikeshoesoutlet.us.com

http://polooutlet.amegov.com

http://www.katespadeshandbags.us

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

http://www.canadagoose.name

http://www.ralph-laurenoutletuk.me.uk

http://www.ralphlaurenoutlets.org.uk

http://www.michaelkorsoutlet-online.in.net

http://www.fredperrypoloshirts.co.uk

http://www.kate-spadeoutlet.us.org

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

http://www.truereligion-outletstore.us.com

http://www.polo-ralphlauren-pascher.fr

http://www.poloralphlauren-uk.me.uk

http://www.mbtshoescheap.us.com

http://www.pradaoutletstore.us

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

http://poloralphlauren.amegov.com

http://www.true-religionoutletstore.us.com

http://www.lebron-jamesshoes.us.com

http://www.cheapuggs.name

http://www.michaelkorsoutlet-clearance.org

http://www.oakleysunglasseswholesales.us.com

http://www.michaelkorsoutlet-clearances.us.com

http://adidasyeezyshoes.adidassale.net

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

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

http://www.longchampoutlet.us.org

http://www.nikeoutlet.com.co

http://www.oakleysunglasseswholesalee.us.com

http://www.oakleysunglassescheaps.us.com

http://www.fredperryoutlet.us.com

http://www.longchampoutletofficial.us.com

http://www.coachfactory-outlet.eu.com

http://www.adidasoutlet.com.co

http://www.asicsrunningshoes.us.com

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

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

http://www.oakleysunglassesformen.us.com

http://www.nhljerseyss.us.com

http://cheapraybansunglasses.co-sunglasses.com

http://www.coachfactoryoutletmiami.us.com

http://www.coachoutletcoachoutletstore.us.com

http://www.adidasoutlet.us.org

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

http://www.michaelkorsoutletofficial.com

http://www.hermesbeltsoutlet.us.com

http://www.pandoraoutletstore.us.com

http://www.michaelkorsoutlet-clearance.us

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

http://www.ralphlaurenoutletonlinestore.us.com

http://www.ralphlaurenofficial.us.com

http://www.michael-korshandbags.in.net

http://www.michaelkorsoutletonlinestore.us.org

http://www.oakleysunglassesfactory.us.com

http://www.polooutlet.us.org

http://www.raybanssun-glasses.com.co

http://www.coachoutletonlinecoachfactoryoutlets.us.com

http://www.michaelkorssoutletclearance.us.com

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

http://nikeshoes.up-nike.com

http://www.poloralph-laurenuk.co.uk

http://www.michael-korsoutletstore.in.net

http://www.coachfactoryoutlet-coachoutlet.us.com

http://www.canadagoosejacketss.com.co

http://www.christian-louboutinshoes.us

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

http://www.cheapnfljerseys.eu.com

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

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

http://www.nike-shoes.us.org

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

http://www.ralph-laurenpolo.us.org

http://www.raybansunglassesonsale90off.us.com

http://www.fitflopssaleclearance.name

http://www.coachoutletcoachfactoryoutlets.us.com

http://www.airjordan.us.org

http://www.canadagoose-outlets.com.co

http://raybansunglassesoutlet.co-sunglasses.com

http://www.uggoutlet.name

http://www.oakleysunglassesss.us.com

http://www.canadagooses.com.co

http://www.ralphlaurenshirts.me.uk

http://www.michaelkorsoutlet-clearance.com

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

http://www.oakleysunglasseswholesaleonline.us.com

http://www.coachoutletonlinecoachfactoryoutlet.com.co

http://www.kate-spadeoutletstore.us.com

http://www.nbajerseyss.us.com

http://yeezyboost.adidassale.net

http://www.nikeoutletstorelocations.com

http://www.montblancpenss.com.co

http://www.pandorajewelrycharms.us.com

http://www.mulberryhandbagsuk.co.uk

http://www.hermesbirkins.us.com

http://www.coachoutlet-clearance.us.org

chenlili20170503

Comment by chenyingying 20 weeks and 2 days ago

Just http://www.huaracherunpas-cher.fr/nike-huarache-taille_49.html what are http://www.huaracherunpas-cher.fr/air-flight-huarache_68.html EMU sneaker?Emu http://www.huaracherunpas-cher.fr/air-huarache-grise_142.html is a type http://www.huaracherunpas-cher.fr/air-huarache-noire_117.html of http://www.huaracherunpas-cher.fr/nike-air-huarache-chaussures_80.html big, flightless, ratite fowl http://www.huaracherunpas-cher.fr/free-huarache_103.html coping http://www.huaracherunpas-cher.fr/huarache-noir_86.html with Sydney with no http://www.huaracherunpas-cher.fr/nike-air-huarache-original_144.html where http://www.huaracherunpas-cher.fr/nike-huarache-id_164.html by otherwise.Oahu is the http://www.huaracherunpas-cher.fr/huarache-montante_179.html countrywide hen http://www.huaracherunpas-cher.fr/achat-nike-huarache_137.html involving http://www.huaracherunpas-cher.fr/nike-air-huarache-light_168.html Sydney.Despite the brand,EMU http://www.huaracherunpas-cher.fr/air-huarache-solde_40.html shoe has nothing http://www.huaracherunpas-cher.fr/huarache-instagram_169.html about http://www.huaracherunpas-cher.fr/huarache-noir-et-blanc_96.html your emu wildlife.Not really http://www.huaracherunpas-cher.fr/air-huarache-og_71.html a solitary http://www.huaracherunpas-cher.fr/nike-air-force-huarache_124.html portion http://www.huaracherunpas-cher.fr/nike-huarache-gris_59.html of the sneaker originate http://www.huaracherunpas-cher.fr/nike-air-huarache-mid_78.html from the http://www.huaracherunpas-cher.fr/chaussures-huarache_56.html particular parrots.However,the actual http://www.huaracherunpas-cher.fr/air-huarache-noir_116.html sneaker as http://www.huaracherunpas-cher.fr/huarache-bordeaux_53.html well as http://www.huaracherunpas-cher.fr/nike-huarache-free-run-pas-cher_156.html the wildlife http://www.huaracherunpas-cher.fr/nike-huarache-1991_55.html are ugly in the http://www.huaracherunpas-cher.fr/nike-huarache-toute-noir_154.html same way.This http://www.huaracherunpas-cher.fr/nike-air-huarache-1991_65.html might http://www.huaracherunpas-cher.fr/basket-huarache_63.html be normally the http://www.huaracherunpas-cher.fr/huarache-5_74.html one link between http://www.huaracherunpas-cher.fr/huarache-bleu_111.html them.EMU http://www.huaracherunpas-cher.fr/nike-air-flight-huarache_102.html running shoe are usually ugg http://www.huaracherunpas-cher.fr/huarache-beige_106.html running shoe, http://www.huaracherunpas-cher.fr/air-huarache-light_139.html much like http://www.huaracherunpas-cher.fr/huarache-noir-et-rose_109.html Ugg boot http://www.huaracherunpas-cher.fr/huarache-4_118.html footwear.Plus they are since http://www.huaracherunpas-cher.fr/nike-air-huarache-2_92.html http://www.huaracherunpas-cher.fr/nike-air-zoom-huarache_89.html unsightly as UGG http://www.huaracherunpas-cher.fr/nike-id-huarache_158.html boot way too.They http://www.huaracherunpas-cher.fr/nike-huarache-3_108.html are built under the http://www.huaracherunpas-cher.fr/nike-huarache-fille_148.html name http://www.huaracherunpas-cher.fr/huarache-vintage_42.html of EMU Quarterly report http://www.huaracherunpas-cher.fr/nike-huarache-solde_94.html ,which http://www.huaracherunpas-cher.fr/nike-air-huarache-safari_73.html is an http://www.huaracherunpas-cher.fr/huarache-light-og_105.html Hawaiian corporation.EMU boot usually http://www.huaracherunpas-cher.fr/nike-huarache-premium_126.html are UGG http://www.huaracherunpas-cher.fr/chaussures-air-huarache_128.html shoeWhat? http://www.huaracherunpas-cher.fr/nike-huarache-magenta_51.html EMU sneaker can be a http://www.huaracherunpas-cher.fr/huarache-run_132.html form http://www.huaracherunpas-cher.fr/nike-air-huarache-turquoise_160.html of ugg boot http://www.huaracherunpas-cher.fr/huarache-air-max_163.html sneaker?This http://www.huaracherunpas-cher.fr/nike-air-huarache-og_162.html can look a http://www.huaracherunpas-cher.fr/nike-huarache-blanc_85.html little bizarre.But it http://www.huaracherunpas-cher.fr/nike-huarache-light-og_115.html is http://www.huaracherunpas-cher.fr/nike-air-huarache-retro_70.html accurate.In Quarterly report,your http://www.huaracherunpas-cher.fr/air-huarache-nike_87.html message uggs is http://www.huaracherunpas-cher.fr/nike-huarache-usa_114.html a universal http://www.huaracherunpas-cher.fr/nike-air-huarache-taille_172.html word http://www.huaracherunpas-cher.fr/chaussure-nike-air-huarache_76.html instead of http://www.huaracherunpas-cher.fr/nike-huarache-agassi_133.html a brand.This presents http://www.huaracherunpas-cher.fr/nike-air-huarache-homme_150.html the kind of http://www.huaracherunpas-cher.fr/nike-air-huarache-run_43.html running shoe which http://www.huaracherunpas-cher.fr/huarache-leopard_77.html are crafted from http://www.huaracherunpas-cher.fr/nike-huarache-safari_52.html twin-face http://www.huaracherunpas-cher.fr/air-huarache-femme_90.html ugg along http://www.huaracherunpas-cher.fr/nike-air-huarache-90_110.html with made of woll inside.According http://www.huaracherunpas-cher.fr/nike-air-huarache-beige_112.html to Rss http://www.huaracherunpas-cher.fr/huarache-og_183.html feeds,throughout http://www.huaracherunpas-cher.fr/nike-huarache-noir-et-bleu_130.html the year http://www.huaracherunpas-cher.fr/nike-air-huarache-international_104.html connected with '06,your http://www.huaracherunpas-cher.fr/air-nike-huarache_107.html http://www.huaracherunpas-cher.fr/nike-air-huarache-light-ultramarine_64.html authorities within australia ruled thatugg http://www.huaracherunpas-cher.fr/acheter-huarache_84.html is http://www.huaracherunpas-cher.fr/nike-huarache-violet_54.html truly a http://www.huaracherunpas-cher.fr/nike-air-huarache-rouge_47.html generic phrase along http://www.huaracherunpas-cher.fr/nike-huarache-rouge_81.html with reported that http://www.huaracherunpas-cher.fr/chaussures-huarache-nike_152.html this needs http://www.huaracherunpas-cher.fr/nouvelle-nike-huarache_82.html to be stripped away http://www.huaracherunpas-cher.fr/huarache-nike-blanche_127.html from the http://www.huaracherunpas-cher.fr/nike-huarache-noir-et-rose_58.html actual signature http://www.huaracherunpas-cher.fr/nike-air-huarache-run-pas-cher_98.html enroll.So,in case http://www.huaracherunpas-cher.fr/nike-huarache-pour-femme_155.html you question the http://www.huaracherunpas-cher.fr/huarache-chaussure_122.html Aussie,I'm http://www.huaracherunpas-cher.fr/nike-huarache-beige_91.html looking to http://www.huaracherunpas-cher.fr/nike-air-force-1-huarache_113.html purchase a http://www.huaracherunpas-cher.fr/nike-huarache-femme-pas-cher_140.html couple of Uggs http://www.huaracherunpas-cher.fr/nike-huarache-run-pas-cher_61.html sneaker,he could http://www.huaracherunpas-cher.fr/nike-huarache-femmes_182.html ask you, http://www.huaracherunpas-cher.fr/nike-air-huarache-noir_97.html Which in turn http://www.huaracherunpas-cher.fr/air-max-huarache_39.html ugg boot http://www.huaracherunpas-cher.fr/huarache-noir-femme_60.html ?Should I http://www.huaracherunpas-cher.fr/nike-air-max-huarache-hybrid_121.html get EMU or maybe http://www.huaracherunpas-cher.fr/ Ugg boot http://www.huaracherunpas-cher.fr/huarache-acheter_83.html sneaker?Technical http://www.huaracherunpas-cher.fr/nike-huarache-marron_165.html communicating,there's http://www.huaracherunpas-cher.fr/nike-huarache-free-run-2012_146.html not a great http://www.huaracherunpas-cher.fr/nike-air-huarache-pas-chere_161.html deal of http://www.huaracherunpas-cher.fr/huarache-usa_100.html among these http://www.huaracherunpas-cher.fr/chaussure-air-huarache_180.html kind of http://www.huaracherunpas-cher.fr/huarache-enfant_62.html footwear.Exact same product,same production http://www.huaracherunpas-cher.fr/nike-huarache-paris_69.html ,same ugliness.Most http://www.huaracherunpas-cher.fr/nike-huarache-2013_159.html is the http://www.huaracherunpas-cher.fr/huarache-nike-chaussures_131.html same.Except that http://www.huaracherunpas-cher.fr/nike-huarache-leopard_57.html they are http://www.huaracherunpas-cher.fr/huarache-nike-pas-cher_79.html tag with various http://www.huaracherunpas-cher.fr/boutique-nike-huarache_151.html brands (beyond http://www.huaracherunpas-cher.fr/nike-air-huarache-prix_120.html Australia).Their price http://www.huaracherunpas-cher.fr/air-huarache-homme_67.html is associated with http://www.huaracherunpas-cher.fr/nike-huarache_93.html not much variations http://www.huaracherunpas-cher.fr/chaussure-huarache-nike_173.html far http://www.huaracherunpas-cher.fr/huarache-nike-noir_149.html too.A couple of http://www.huaracherunpas-cher.fr/nike-air-flight-huarache-og_48.html regular Uggs footwear http://www.huaracherunpas-cher.fr/huarache-2017_99.html advertise at http://www.huaracherunpas-cher.fr/basket-nike-huarache_45.html about $140 and http://www.huaracherunpas-cher.fr/nike-huarache-prix_66.html EMU about http://www.huaracherunpas-cher.fr/nike-air-huarache-solde_181.html $150.Both footwear will http://www.huaracherunpas-cher.fr/air-huarache-paris_138.html be sold in http://www.huaracherunpas-cher.fr/nike-air-huarache-run-chaussures_174.html Quarterly http://www.huaracherunpas-cher.fr/nike-huarache-homme_141.html report,All of us http://www.huaracherunpas-cher.fr/huarache-free-run_145.html along with http://www.huaracherunpas-cher.fr/nike-air-huarache-og-retro_101.html European http://www.huaracherunpas-cher.fr/air-huarache-blanc_123.html countries.So acquiring either http://www.huaracherunpas-cher.fr/nike-huarache-ultramarine_125.html one wont be a http://www.huaracherunpas-cher.fr/nike-huarache-free-light_134.html difficulty.My advice is http://www.huaracherunpas-cher.fr/nikeair-huarache_88.html actually a http://www.huaracherunpas-cher.fr/huarache-light_50.html person http://www.huaracherunpas-cher.fr/nike-huarache-jd_46.html a brandname follower next http://www.huaracherunpas-cher.fr/nike-air-huarache-light-pas-cher_44.html obtain a http://www.huaracherunpas-cher.fr/nike-huarache-chaussures_129.html set of regardless http://www.huaracherunpas-cher.fr/huarrache-blanche_135.html of what you http://www.huaracherunpas-cher.fr/nike-huarache-boutique_147.html are http://www.huaracherunpas-cher.fr/huarache-chaussures_95.html feeling such http://www.huaracherunpas-cher.fr/huarache-og-2017_143.html as.Equally sneaker have identical http://www.huaracherunpas-cher.fr/air-huarache-run_157.html real softness http://www.huaracherunpas-cher.fr/nike-huarache-light-ultramarine_171.html and heat to http://www.huaracherunpas-cher.fr/nike-air-huarache-bleu_167.html the http://www.huaracherunpas-cher.fr/nike-huarache-run_72.html ft.Whats much more, http://www.huaracherunpas-cher.fr/nike-air-huarache-light-ultramarine-og_75.html in the event http://www.huaracherunpas-cher.fr/nike-air-huarache-blanc_41.html you http://www.huaracherunpas-cher.fr/huarache-femmes_170.html don some EMU,no body http://www.huaracherunpas-cher.fr/nike-air-huarache-blanche_153.html http://www.huaracherunpas-cher.fr/nike-huarache-2_166.html will know it's not Ugg sheepskin boots.

Comment by scott154 2 weeks and 4 days ago

Your point of view

You must be signed in to comment.