How can I count all the lines of code in a directory recursively?
Try:
find . -name '*.php' | xargs wc -l
or (when file names include special characters such as spaces)
find . -name '*.php' | sed 's/.*/"&"/' | xargs wc -l
The SLOCCount tool may help as well.
It will give an accurate source lines of code count for whatever
hierarchy you point it at, as well as some additional stats.
Sorted output:
find . -name '*.php' | xargs wc -l | sort -nr
How to count lines of code including sub-directories
First you do not need to use cat
to count lines. This is an antipattern called Useless Use of Cat (UUoC). To count lines in files in the current directory, use wc
:
wc -l *
Then the find
command recurses the sub-directories:
find . -name "*.c" -exec wc -l {} \;
.
is the name of the top directory to start searching from-name "*.c"
is the pattern of the file you're interested in-exec
gives a command to be executed{}
is the result of the find command to be passed to the command (herewc-l
)\;
indicates the end of the command
This command produces a list of all files found with their line count, if you want to have the sum for all the files found, you can use find to list the files (with the -print
option) and than use xargs to pass this list as argument to wc-l.
find . -name "*.c" -print | xargs wc -l
EDIT to address Robert Gamble comment (thanks): if you have spaces or newlines (!) in file names, then you have to use -print0
option instead of -print
and xargs -null
so that the list of file names are exchanged with null-terminated strings.
find . -name "*.c" -print0 | xargs -0 wc -l
The Unix philosophy is to have tools that do one thing only, and do it well.
How can I count all the lines of code in a directory recursively?
Try:
find . -name '*.php' | xargs wc -l
or (when file names include special characters such as spaces)
find . -name '*.php' | sed 's/.*/"&"/' | xargs wc -l
The SLOCCount tool may help as well.
It will give an accurate source lines of code count for whatever
hierarchy you point it at, as well as some additional stats.
Sorted output:
find . -name '*.php' | xargs wc -l | sort -nr
Total number of lines in a directory
If what you want is the total number of lines and nothing else, then I would suggest the following command:
cat * | wc -l
This catenates the contents of all of the files in the current working directory and pipes the resulting blob of text through wc -l
.
I find this to be quite elegant. Note that the command produces no extraneous output.
UPDATE:
I didn't realize your directory contained so many files. In light of this information, you should try this command:
for file in *; do cat "$file"; done | wc -l
Most people don't know that you can pipe the output of a for
loop directly into another command.
Beware that this could be very slow. If you have 100,000 or so files, my guess would be around 10 minutes. This is a wild guess because it depends on several parameters that I'm not able to check.
If you need something faster, you should write your own utility in C. You could make it surprisingly fast if you use pthreads.
Hope that helps.
LAST NOTE:
If you're interested in building a custom utility, I could help you code one up. It would be a good exercise, and others might find it useful.
Count lines of code in directory using Python
from os import listdir
from os.path import isfile, join
def countLinesInPath(path,directory):
count=0
for line in open(join(directory,path), encoding="utf8"):
count+=1
return count
def countLines(paths,directory):
count=0
for path in paths:
count=count+countLinesInPath(path,directory)
return count
def getPaths(directory):
return [f for f in listdir(directory) if isfile(join(directory, f))]
def countIn(directory):
return countLines(getPaths(directory),directory)
To count all the lines of code in the files in a directory, call the "countIn" function, passing the directory as a parameter.
How to get the line-count of all the files in a directory
Thy this:
find ./pathToDirectory -type f -exec wc -l {} +
CLOC - how to count loc in a specific (unique) directory using match-d
Use a combination of --fullpath
(to let the filter apply to the entire directory path instead of just the trailing directory name) and positive or negative look-aheads in --match-d
to isolate the /gen/
subdirectory of interest.
If you post a sample directory structure you're working with and identify the directories you want and don't want, I will take a shot at writing a --match-d
expression to meet your needs.
Related Topics
How to Extract Only the Raw Contents of an Elf Section
Making Cmake Print Commands Before Executing
Linking 32-Bit Library to 64-Bit Program
Prevent Gnome Terminal from Exiting After Execution
How to Decode /Proc/Pid/Pagemap Entries in Linux
Sorting on the Last Field of a Line
Signing Windows Application on Linux-Based Distros
How to Monitor a Complete Directory Tree for Changes in Linux
Need a Good Hex Editor for Linux
How Is the Linux Kernel Tested
Does Linux Schedule a Process or a Thread
Why Is the Page Size of Linux (X86) 4 Kb, How Is That Calculated
The Concept of 'Hold Space' and 'Pattern Space' in Sed
Differencebetween Double-Ampersand (&&) and Semicolon (;) in Linux Bash
Docker Volume Not Mounting Any Files