Sometimes you may required to count number of lines in a file on Linux command line or shell scripting. This tutorial will help you to count number of lines in a file on Linux.
Please change <filename> value with your actual file name and it will return number of lines in a file as output.
Following command will count number of lines in /etc/passwd files and print on terminal. We can also use –lines in place of -l as command line switch.
wc -l /etc/passwd
You can also count number of line on piped output.
cat /etc/passwd | wc -l
Count Total Character’s in a File
Use -m or –chars switch with wc command to count number of characters in a file and print on screen.
wc -m /etc/passwd
Count Total Words in a File
Use -w or –words switch with wc command to count number of words in a file and print on screen.
wc -w /etc/passwd