1

Is it possible to return a combined word count with wc only for certain files (like .txt files, for example) in a series of directories?

  • 6
    cat them all and pipe the result to wc ? – don_crissti Mar 25 '17 at 20:12
9

With GNU wc (at least), you can combine the results of find with wc as such:

find folder/ -name '*.txt' -print0 |   wc -w --files0-from=-

This gives you all the power of find (a bit overkill if you just want to find all files ending with .txt to be honest) and it handles even the strangest filenames (containing newlines for example).

0

Do I understand your situation correctly? So, you're saying that you are in a folder say Documents, and you have several folders within this folder, so you change directory (cd) into that directory an want to count how many .txt files are deep into that directory in every sub-folder?

If that's what you mean, then this is how you do it:

$ ls -R *.txt | wc -l

ls = list. -R = recursively every subfolder *.txt = any number of any character ending in .txt

| = pass to.

wc -l = count how many.

  • This doesn't do what you think it does. The *.txt would be expanded by the shell into a space delimited list of .txt files found in the current working directory. If no .txt files exist, ls would likely (depending on shell options) get passed *.txt, but since ls doesn't do pattern matching, it would be looking for a file litterarly called *.txt in the current working directory. – BroSlow Mar 26 '17 at 2:16
  • I think you are wrong. – xyz123 Mar 26 '17 at 2:56
  • See how bash processes commands and file name expansion. ls *.txt is generally equivlient to echo *.txt. The shell is doing the expansion first, ls is essentially just printing the result. – BroSlow Mar 26 '17 at 3:50
  • Oh, so what the asker wanted was the number of lines in every text file recursively, and not the number of text files recursively? – xyz123 Mar 26 '17 at 4:06
  • Yes, but the *.txt is processed, before the ls -R. Your command doesn't count the number of .txt files recursivly. It counts the number of .txt files in the current working directory and fails on some edge cases. ls -R | grep .txt | wc -l would be closer, but again doesn't handle newlines well, etc... lgeorget's awnser does – BroSlow Mar 26 '17 at 4:54
0

Here is a good one-liner via xargs I just made - tested on macOS:

find . | grep -E "(.*\.js.*|.*\.txt|.*\.css|.*\.htm.*|.*\.sh|.*\.scss|.*\.md|.*\.java|.*\.conf|.*\.yaml)" | sed "s/^/\"/" | sed "s/$/\"/" | xargs -J thefile wc thefile

This handles spaces in filenames. Maybe swap -J for -Y in Linux? Lawn mower cut my Ethernet to my linux box so I can't test there presently.

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.