0

This is how find files using find and show the number of lines in each file

$ find ./ -type f -name "data*.csv" -exec wc -l {} +
   380723 ./data_2016-07-07-10-41-13.csv
   369869 ./data_2016-07-11-10-42-01.csv
   363941 ./data_2016-07-08-10-41-50.csv
   378981 ./data_2016-07-12-10-41-28.csv
  1493514 total

how do I sort the results by file name? Below is my attempt, but it is not working.

$ find ./ -type f -name "data*.csv" -exec wc -l {} + | sort
1493514 total
363941 ./data_2016-07-08-10-41-50.csv
369869 ./data_2016-07-11-10-42-01.csv
378981 ./data_2016-07-12-10-41-28.csv
380723 ./data_2016-07-07-10-41-13.csv
$ find ./ -type f -name "data*.csv" | sort -exec wc -l {} +
sort: invalid option -- 'e'
Try `sort --help' for more information.
$ find ./ -type f -name "data*.csv" -exec sort | wc -l {} +
find: wc: {}missing argument to `-exec'
: No such file or directory
wc: +: No such file or directory
0 total
$

Can someone offer a solution and correct me so I understand it better?

EDIT1

from man sort

  -k, --key=POS1[,POS2]
          start a key at POS1 (origin 1), end it at POS2 (default end of line).  See POS syntax below


   POS is F[.C][OPTS], where F is the field number and C the character position in the field; both are origin 1.  If neither -t nor -b is in effect, characters in a field are counted from the begin‐
   ning of the preceding whitespace.  OPTS is one or more single-letter ordering options, which override global ordering options for that key.  If no key is given, use the entire line as the key.
HattrickNZ
  • 4,373
  • 15
  • 54
  • 98

1 Answers1

3

Ismail's suggestion of using sort -k is correct. However, I'm often too lazy to learn (or relearn) how -k works, so here's a cheap solution:

find . -name 'data*.csv' -print0 | sort -z | xargs -0 wc -l

Edit: after some experimentation, I did figure out how -k works:

find . -name 'data*.csv' -exec wc -l {} + | sort -k 2
C. K. Young
  • 219,335
  • 46
  • 382
  • 435