-1

I would like to have a script that goes through every single file in a directory (that includes sub directories) and return me the total number of lines in all files excluding blank lines and excluding folders/files that have key words on them (log).

I currently have a line of code which returns me the total number of lines (including blank lines) in files of a single directory 'files'

cat /home/user/files/* | wc -l

Is there someway to implement so that I can call a code which can read through, for example:

- /home/user/files/*
- /home/user/files/files2/*
- /home/user/files/files2/files3/*

a bunch of directories (unknown number) and return me the total number of lines of all files (excluding blank lines) (and excluding folders/files that have key words on them (log)) found.

I'm currently using BASH to script but if there is an solution in python, I'm more than happy to use it.

carol Alex
  • 353
  • 1
  • 3
  • 15
  • @juanpa.arrivillaga i've updated my question slightly, i would need the total number of lines excluding blank lines. – carol Alex Sep 15 '16 at 06:22

1 Answers1

1

Try finding all the files in the directory recursively ( -type f flag to find). Beware that this will print out binary files too, as does your current code.

find /home/user/files/ -type f -exec cat {} \; | wc -l

Edit: the --files-from solution given in Use wc on all subdirectories to count the sum of lines is more efficient, as linked to in the question comments

Community
  • 1
  • 1
jedifans
  • 2,287
  • 1
  • 13
  • 9
  • thanks for your edit. i was just about to ask you the differences between your solution and the solution given in http://stackoverflow.com/questions/13727917/use-wc-on-all-subdirectories-to-count-the-sum-of-lines – carol Alex Sep 15 '16 at 06:09
  • apologies for unaccepting your answer, i've updated my question slightly as i forgot to add it in earlier - i would need it to return me the total number of lines (excluding blank lines) – carol Alex Sep 15 '16 at 06:23
  • No problems. Is it code files that you are counting? If so, there's quite a few lines of code (loc) analysis tools out there for use on continuous integration servers. – jedifans Sep 15 '16 at 06:26
  • yes! i am essentially trying to count code. i'll have a look at them. would you also know how i could avoid counting certain folders/files with keywords on them? (i.e, 'log') – carol Alex Sep 15 '16 at 06:28
  • Depends on the tool. Some provide options to exclude, some don't. If it comes to it, pre-filter with `find/home/user/files/ -type f ! -path '*/log/*'` – jedifans Sep 15 '16 at 06:33