How would I calculate and display the number of lines and words that are contained in a .sh file?
Asked
Active
Viewed 2.2e+01k times
4 Answers
162
Use the program wc.
To count the number of lines:
-lwc -l myfile.shTo count the number of words:
-wwc -w myfile.sh
See man wc for more options.
unrealapex
- 125
sourav c.
- 44,715
31
As mentioned by souravc, you can use wc for this:
$ wc -w statusToFiles.sh
10 statusToFiles.sh
$ wc -l statusToFiles.sh
6 statusToFiles.sh
To only display the count itself, you can pipe that output to awk, like this:
$ wc -l statusToFiles.sh | awk '{ print $1 }'
6
...or as kos mentioned below:
$ < statusToFiles.sh wc -l
6
Aaron
- 6,714
21
You can use grep command with blank matching string
grep "" -c file_path
andreykyz
- 763
-
2Why does this is answer not have more upvotes? Is there something fishy about it? It works like a charm for me and the code looks very simple. – Felix Crazzolara Nov 02 '19 at 11:04
-
1or with non-blank matching string if you want to count non-empty lines
grep -c . file(The period . matches any single character.) – jakun May 29 '20 at 16:01 -
1
-
1
-
8
You can also output the entire file with line numbers in front of every line using the command below:
cat -n myfile
Byte Commander
- 107,489
imad
- 89
-
This is useless for big files (e.g. 250MB), instead it may causing freeze – user2342558 Jun 29 '20 at 12:39
-loption, so if your file have three lines (two \n), thus,wc -l myfile.shwill return 2. – ViniciusArruda Feb 17 '22 at 14:36