When you are managing massive server logs, analyzing giant CSV datasets, or reviewing thousands of lines of source code in a Linux environment, you often need to know exactly how large the file is. While you could open the file in a text editor and scroll all the way to the bottom to look at the line number, doing so with a 5-gigabyte log file will instantly crash your terminal. Instead, you should use the incredibly fast wc (word count) command.
Using the ‘wc’ Command
The wc utility is pre-installed on virtually every Linux distribution and is designed to count bytes, words, and lines instantly, without actually loading the entire file into memory.
- Open your terminal application.
- Type the following command, using the
-l(lines) flag:
wc -l /path/to/your/file.txt - Press Enter.
The terminal will instantly output a single number followed by the file name (e.g., 45218 file.txt). This tells you there are exactly 45,218 lines of text inside that document.
Counting Multiple Files Simultaneously
You can also use this command to calculate the total size of an entire project. If you have a folder full of Python scripts and want to know how many lines of code you have written in total, simply run:
wc -l *.py
The terminal will list the exact line count for every individual Python file, and then provide a grand “total” sum at the very bottom.