Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting line count of all files in project in sorted order.(Bash terminal)

I have this folder contiaining a lot of *.cpp and *.h files. I want to get a line count of all the files and display them to the screen in sorted order of line count.

I know wc -l *.h *.cpp will display all the file names with line count, but not in sorted order.

like image 402
smilingbuddha Avatar asked Dec 14 '11 17:12

smilingbuddha


People also ask

How do you count all lines in all files in a directory?

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 line numbers in bash?

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.

Which command is used to list all the files in sorted order?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

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

Countings Millions of Lines of Files As an example, the Linux kernel has many directories, subdirectories, and thousand of files. In such a case, the find command splits the list of files into pieces and makes the wc command print a total for each sublist rather than a total for the entire list.


1 Answers

Use wc -l *.h *.cpp | sort -n. One job (sort), one tool (sort).

like image 195
thiton Avatar answered Oct 09 '22 15:10

thiton