I have a folder full of .doc or .docx files. I'd like to know how many words--or, failing that, characters or lines--are in each file without opening each one of them. (Use case: I'm a TA who needs to grade a ton of reading responses each week for completion.)

Is there some way to do this from command line? I'd eventually like to get all of this in a .csv file. I'm happy to download some utility to do this.

I run Arch linux, if that matters.


Sorry, I thought I forgot to add this code at the beginning:

cp -Rp doument_directory document_directory.orig

cd document_directory
for file in *
  docx2txt "${file}" > "${file}.txt" && mv "${file}.txt" "${file}.doc"

if your system doesn't have docx2txt utility, you can get it from http://sourceforge.net/projects/docx2txt/

Caveat emptor: Line break formatting of MS Word documents may (and most probably will) differ from that of UNIX. So the line count may not be accurate but, these should do what you are looking for :

 wc file.doc

you will see an output like this :

 28 377 1492

First number is the number of lines, second, number of words and third is the number of characters.

If you want to do only one type of count at a time, you can do:

For number of lines

wc -l file.doc

For number of words

wc -w file.doc

For number of characters

wc -c file.doc

And you will get a single number followed by the file name.

  • So now I've figured out how to get docx2txt to work (the shell script is docx2txt.sh for some reason from AUR), I'd like to see if there's a way around converting and reading all of these files. The docx2txt documentation, sparse as it is, mentions there's a way to make the output of docx2txt as STDOUT. Does that mean that I could pipe the text file to wc? That would be ideal. But I can't figure out what syntax they're talking about on the man page! Even if not, I suppose I could go with your solution. – Pierce Jan 14 '16 at 4:43
  • @Pierce From looking at the source of doc2txt it looks like the output is sent to stdout. What error do you get when you do doc2txt myfile.doc | wc? – gardenhead Jan 14 '16 at 6:09
  • When I type docx2txt.sh <file name>.docx | wc, I get some numbers that don't match what's in the document, and don't seem to vary when I add lines, words, or characters to the document (and create a new .txt file). The output of wc <file name>.txt for the file created by docx2txt seems correct. – Pierce Jan 14 '16 at 14:07

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.