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.
How to count lines in a document?
Use wc
:
wc -l <filename>
This will output the number of lines in <filename>
:
$ wc -l /dir/file.txt
3272485 /dir/file.txt
Or, to omit the <filename>
from the result use wc -l < <filename>
:
$ wc -l < /dir/file.txt
3272485
You can also pipe data to wc
as well:
$ cat /dir/file.txt | wc -l
3272485
$ curl yahoo.com --silent | wc -l
63
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 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 to get the line-count of all the files in a directory
Thy this:
find ./pathToDirectory -type f -exec wc -l {} +
Number of lines in all files in a directory
use SLOCCOUNT a well-known and free source line of code counting.
It supports COBOL and use COCOMO model to estimate effort and schedule.
How To Get Total Line Count From All Text Files In A Directory
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
SET /a countfiles=0
SET /a countlines=0
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*" '
) DO (
SET /a countfiles+=1
FOR /f "delims=" %%c IN ('find /c /v "~~~" "%sourcedir%\%%a"') DO CALL :setcount %%c
)
SET cou
GOTO :EOF
:setcount
IF "%2" neq "" shift&GOTO setcount
SET /a countlines +=%1
GOTO :EOF
You would need to change the setting of sourcedir
to suit your circumstances.
For each filename found in the directory, count a file, then deliver to the subroutine setcount
the output of the find
command, typically
---------- U:\SOURCEDIR\T W O\FILE4.TXT: 3
Since we want the last item in this string, we simply shift the parameter until the second does not exist, hence the first must be the linecount from find
, so add it.
Related Topics
Difference Between --Cap-Add=Net_Admin and Add Capabilities in .Yml
Bash Alias Create File with Current Timestamp in Filename
Rsync --Exclude Not Excluding Specific Files
Extract Text Between Two Strings Repeatedly Using Sed or Awk
How to Manage Log Verbosity Inside a Shell Script
How to Install Valgrind Properly
How to Schedule an R Script Cronjob in a Linux Server
How to Convert a Linux Executable File (Binary) to Windows Exe File
32-Bit Process's Address Space on 64-Bit Linux
Why Does "Uniq" Count Identical Words as Different
Bash Script: Always Show Menu After Loop Execution
How to Prevent Tar from Overwriting an Existing Archive
How to Get No. of Lines Count That Matches a String from All the Files in a Folder
Light-Weight SQL Server for Linux/Windows
Use Sed to Delete Certain Lines Using an Index with the Line Numbers to Delete