Total number of lines in a directory

后端 未结 7 2069
礼貌的吻别
礼貌的吻别 2021-01-13 08:52

I have a directory with thousands of files (100K for now). When I use wc -l ./*, I\'ll get:

 c1            ./test1.txt
 c2            ./tes         


        
7条回答
  •  死守一世寂寞
    2021-01-13 09:29

    (Apologies for adding this as an answer—but I do not have enough reputation for commenting.)

    A comment on @lifecrisis's answer. Perhaps cat is slowing things down a bit. We could replace cat by wc -l and then use awkto add the numbers. (This could be faster since much less data needs to go throught the pipe.)

    That is

    for file in *; do wc -l "$file"; done | awk '{sum += $1} END {print sum}'
    

    instead of

    for file in *; do cat "$file"; done | wc -l
    

    (Disclaimer: I am not incorporating many of the improvements in other answers, but I thought the point was valid enough to write down.)

    Here are my results for comparison (I ran the newer version first so that any cache effects would go against the newer candidate).

    $ time for f in `seq 1 1500`; do head -c 5M myfile-$f |sed -e 's/\(................\)/\1\n/g'; done
    
    real    0m50.360s
    user    0m4.040s
    sys 0m49.489s
    
    $ time for file in myfile-*; do wc -l "$file"; done | awk '{sum += $1} END {print sum}'
    30714902
    
    real    0m3.455s
    user    0m2.093s
    sys 0m1.515s
    
    $ time for file in myfile-*; do cat "$file"; done | wc -l
    30714902
    
    real    0m4.481s
    user    0m2.544s
    sys 0m4.312s
    

提交回复
热议问题