Bash count lines in directory The `wc -l` command in Bash is used to count the number of lines in a file or input, providing a quick way to determine the line count of text data. Made with love and Ruby on Rails. -name pattern to find some files, and I'd like to elegantly get the total number of lines in these files. It is particularly useful for developers who want to analyze codebases, track productivity, or determine the size of software projects. Dec 8, 2016 · In Bash, how do I count the number of non-blank lines of code in a project? I'm running a find . I find this to be quite elegant. -name '*. Discover efficient techniques to effortlessly tally lines in your files. Although primarily designed for analyzing text, you can combine it with commands like find, ls, and xargs to count files in directories and perform other data-processing tasks. In this situation, though, we are using this command to count the number of files in a directory. Aug 24, 2015 · What is the Windows equivalent of the Linux/Unix command wc -l? Basically, how do you count the number of lines output from a command on the Windows command line? May 24, 2019 · I have a few hundred txt files in several subfolders and I would like the count the lines in each txt file. I want to count the lines of all files in this directory and all its subdirectories, but exclude directories "public", "modules", and "templates". Be happy. UPDATE: I didn't realize your directory contained so Jun 8, 2016 · You should probably use SLOCCount or cloc for this, they're designed specifically for counting lines of source code in a project, regardless of directory structure etc. Built on Forem — the open source software that powers DEV and other inclusive communities. For thi Aug 23, 2010 · I have a directory with a large number of files. The wc (word count) command is one of the most useful text processing utilities in Linux and Unix systems. com Jul 22, 2024 · This tutorial explains how to use Bash to count the number of lines in each file of a specific directory, including examples. Sep 6, 2023 · The "ls" command on Linux lists files and directories. The script Sep 11, 2025 · Learn how to use the Linux wc command to count lines, words, characters, and bytes in files, making text analysis and management easier. It is a powerful utility that allows you to count lines, words, characters, and bytes in one or more files. How? Oct 31, 2017 · If what you want is the total number of lines and nothing else, then I would suggest the following command: cat * | wc -l This concatenates the contents of all of the files in the current working directory and pipes the resulting blob of text through wc -l. What is the best way to do this? Mar 18, 2024 · In this section, we’ll use the find command along with wc and sort to discover all the files within a directory and then list them by the number of lines they contain. This command is particularly useful for those working with I am redirecting grep results to a file, and then using cat to show its contents on the screen. Apr 10, 2025 · Quick overview of counting up lines inside the directory for all files. 1. The "tree" command displays the entire directory structure recursively and summarizes file counts at the end. After that, wc command is used with --lines argument to count the number of lines, and similarly, wc command with --words argument is used to count the number of words in Jul 13, 2020 · But I want to just print the average of the number of lines for the whole directory instead of calculating the average from the txt file output instead. Feb 18, 2009 · I'm trying to find a way to count the total number of lines in all of the source files of a project I have. ; either sloccount . This guide shows both command-line tools and GUI methods. txt. Jan 1, 2024 · To count the total number of files in a directory recursively using find, you can use the command find /path/to/directory -type f | wc -l. furthermore, with ls you are counting directories as well as files. echo "Provider folder argument please. Here is an example from a file in bash. How can I achieve that? Mar 23, 2015 · Assuming I want to target a specific file extension in the target directory (and all sub directories) what might be an effective way to recursively count the number of line breaks within a folder (and sub folders) from the Windows command line? How to count the number of folders in a specific directory. There is a little tool called sloccount to count the lines of code in a directory. Follow all symbolic links, unlike -r. This is the default when there is only one file (or only standard input) to search. Dec 17, 2024 · cloc is a versatile command-line tool designed to efficiently count the number of lines of code (LOC) in a set of files or an entire directory. I've looked at an awk command awk 'END{print NR}' directory/* which returns the number of lines in the entire directory. If all of the files are in the same directory I can execute: cat * | wc -l However, if there are sub-directories, this doesn't work. To do this, run the "ls" command on the "/etc" directory and pipe it into the "wc" command. I can do this for all txt files in the current directory by using: for f in *. If a parameter is provided as an input, script should only check number of lines for that input file, otherwise script should display number of lines for each file within that directory. The tool supports multiple programming languages and distinguishes between code lines, comment lines, and blank Master the art of bash count lines with our quick guide. May 11, 2024 · Fortunately, Linux offers a variety of efficient ways to count the lines of a file in Bash. The "find" command with options like -type, -mindepth, and -maxdepth can recursively count Jul 23, 2025 · Using wc command Explanation: The first line tells the system that bash will be used as an interpreter. See full list on baeldung. -R, --dereference-recursive Read all files under each directory, recursively. find "$1" -type f -exec cat {} + | wc -l fi } Thanks for your attention. sh Linux Bash Script Now lets run cloc on it. I want to know how many lines of results I have in my results file and then add it to some counter. Keep track of your data and manage storage efficiently. It provides essential statistics about files, including the number of lines, words, and characters. Adding the "-a" option displays hidden files as well. Is there some command line magic to get a count of files? Is there a bash command which counts the number of files that match a pattern? For example, I want to get the count of all files in a directory which match this pattern: log* May 22, 2025 · The wc (word count) command is a powerful utility in Linux used to count lines, words, characters, and bytes in files. It should be noted that it does more than you want as it ignores empty lines/comments, groups the results per programming language and calculates some statistics. To use cloc simply type cloc followed by the file or directory which you wish to examine. Whether you’re analyzing log files, counting code lines, or processing text data, the wc command is an indispensable tool for system administrators, developers, and data analysts. It can be applied across different scenarios where counting these elements is necessary, whether for file analysis, data stream management, or simply understanding the content of a text file. The grep, therefore, counts the number of lines matching anything (''), so essentially just counts the lines. I don't see a ls switch to provide the count. txt What is `wc`? The `wc` command in Bash stands for word count. Jun 30, 2025 · Counting files in a Linux directory is essential for system and storage management. A variable is created to hold the file path. However, the methods work exactly the same in Windows 10 too. Aug 7, 2019 · On Linux and Unix-like operating systems, the wc command allows you to count the number of lines, words, characters, and bytes of each given file or standard input and print the result. I've tried piping dir -r -name into measure-object -line, but that just counts the number Mar 2, 2024 · Learn how to easily count files and directories in Linux using commands like find, ls, grep, and wc. 2. If you want to use find and wc, GNU wc has a nice --files0-from option: find . Discover how to bash count files in directory effortlessly. In this tutorial, we’ll look at the most common ways of counting the number of lines of a specified file in Bash. or cloc . Assume you wish to count the number of files in the /etc directory. txt; do wc I have 40 files in a directory and I want to count the number of times there is a line with a "2" in the first column in each file individually. Any assistance would be greatly greatly appreciated Feb 6, 2012 · The question is about recursively counting files from a directory forward and the command you show does not do that. Learn to install and apply it to multiple programming languages. wc -l filename. md o lines of code in files Nov 25, 2008 · Suppose I want to count the lines of code in a project. Piping it to "wc -l" counts the total files and directories, including hidden ones. The file in question contains the following code in bash: $ cat bash_script. I have considered a printf solution to format the wc -l output . Oct 5, 2025 · File Explorer, PowerShell, and the Command Prompt: NOTE: In this guide, I’m using Windows 11 to illustrate the methods for counting files, folders, and subfolders inside a directory. I am using the following command, but it always provides an extra one. [ch]' -print0 | wc --files0 May 1, 2020 · Question I have a git repository full of files, mostly text. Jul 14, 2023 · Cloc can be used to count lines in particular file or in multiple files within directory. How to use File Explorer to get the number of files in a folder Do […]. I am trying something like this, but it prints out Aug 27, 2022 · 60 6 3 7 etc, where the first column represents number of files and the second represents number of lines. Jul 23, 2025 · The wc command is used in Linux to output the number of bytes, characters, or newlines. Dec 17, 2024 · The wc command, short for “word count,” is a versatile tool in Unix-based systems used to count lines, words, and bytes in files or data streams. py m lines of code in files ending in . The wc command is used to find out the number of lines and number of words. This concise guide unveils simple commands to streamline your directory insights. This command will find all regular files in the specified directory and its subdirectories recursively and count the number of files using the wc -l command. I want to know that there are n lines of code in files ending in . Apr 8, 2011 · I want to find the total count of the number of files under a folder and all its sub folders. " else. Jul 30, 2024 · This tutorial explains how to group by and count lines in Bash, including an example. Sep 22, 2025 · Use cloc to efficiently count lines of code on Linux systems. will produce a report on all the source code starting from the current directory. Note that the command produces no extraneous output. Example File Setup To illustrate the different methods for counting lines in a file, we’ll use a sample file named countries. d7rn topx8 gsdh wyd lk ijoumx xwqwub cejx gco pyd