I know how to get length of the longest line in a text file with awk

awk ' { if ( length > L ) { L=length} }END{ print L}' file.txt

but how can I get the length of the longest line of all files in a directory?


The most straightforward solution is to concatenate all the files and pipe the result to your script:

cat ./* | awk '{ if ( length > L ) { L=length} }END{ print L}'

You can also pass directly several files to awk:

awk '{ if ( length > L ) { L=length} }END{ print L}' ./*

Of course, there can be some warnings if files are in fact directories but it should be harmless. You may have bigger problems with binary files because they don't have a concept of line. So, in order to be more specific, you can do something like

 awk '{ if ( length > L ) { L=length} }END{ print L}' ./*.txt

to match only the .txt files in the current directory.

And, as @G-Man stated in his comment, * won't match hidden files (starting with a dot). If you want those, use * .*.

  • Or, arguably, cat * .* | .... Or eliminate the useless use of cat and say awk '...' * .*. – G-Man Aug 18 '14 at 15:07
  • @G-Man Indeed. I was editing my answer about that when you commented. :-) I said most straightforward because concatenating the files with cat is what seemed simpler at first. – lgeorget Aug 18 '14 at 15:09

With GNU wc:

cat *.txt|wc -L

-L prints the length of the longest line.


If you want the max length per file, with GNU awk:

find . -type f -exec awk -v l=0 '
   length>l {l=length} ENDFILE{print FILENAME ":", l; l=0}' {} +

Or the one max length in all the files:

find . -type f -size +1c -exec cat {} + |
  awk -v l=0 'length>l {l=length}; END{print l}'

That assumes the files end in newline characters. If one file doesn't end in a newline character, then its last non-delimited line will be merged with the first line of the next file and possibly void your result.

-size +1c is an optimisation as text files that are empty or contain only one character have respectively 0 line and 1 empty line, so won't have the longest line.

  • They're text files, so they do end in newline characters (unless empty). – Gilles Aug 18 '14 at 21:14

Also with GNU wc (coreutils 8.4), it can handle multiple files

wc -L *.txt
  • lists the length of each individual file
  • as well as provide the longest length of all files
  • This is essentially what this other answer mentioning wc -L does, but your solution has the disadvantage that you have to wade through the max-length of all the other files first. Is there any advantage in that? – Anthon Feb 13 '15 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.