Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use wc on all subdirectories to count the sum of lines

How can I count all lines of all files in all subdirectories with wc?

cd mydir wc -l * .. 11723 total 

man wc suggests wc -l --files0-from=-, but I do not know how to generate the list of all files as NUL-terminated names

find . -print | wc -l --files0-from=- 

did not work.

like image 993
Jonas Stein Avatar asked Dec 05 '12 16:12

Jonas Stein


People also ask

How do you count lines in wc?

wc. The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.

How do I count the number of lines in a directory in Linux?

The easiest way to count files in a directory on Linux is to use the “ls” command and pipe it with the “wc -l” command. The “wc” command is used on Linux in order to print the bytes, characters or newlines count. However, in this case, we are using this command to count the number of files in a directory.

How do I count the number of lines in multiple files in Linux?

First, the find command fetches all C language files and header files in the src and include directories, respectively. Secondly, all files are passed one by one to wc command via xargs. So the wc command will perform the count of the number of lines for each file.


1 Answers

You probably want this:

find . -type f -print0 | wc -l --files0-from=- 

If you only want the total number of lines, you could use

find . -type f -exec cat {} + | wc -l 
like image 78
gniourf_gniourf Avatar answered Oct 01 '22 03:10

gniourf_gniourf