When reviewing configuration files, server logs, or blocks of programming code in the Linux terminal, finding specific data can be difficult. If a compiler throws an error stating that there is a syntax issue on “line 142”, opening the file with the standard cat command is unhelpful because it dumps the text onto your screen without any point of reference.
While you could open the file in a full text editor like nano or vim to find the line, this is often overkill for simply reading a file. The most efficient way to read a file while simultaneously displaying line numbers is by using the native nl (number lines) command.
What is the nl Command?
The nl command is a core utility included in almost all Linux distributions, Unix systems, and macOS. Its primary function is to read a file, prepend a line number to each line of text, and output the result directly to standard output (your terminal screen).
Importantly, the nl command does not modify the original file. It only modifies how the text is displayed on your screen, making it completely safe to use on critical system configuration files.
How to Use the Basic nl Command
To number the lines in a file, simply type the command followed by the filename.
nl /var/log/syslog
The terminal will print the contents of the file, with clean, right-aligned numbers sitting next to the text. However, you will notice a specific quirk: by default, the nl command completely ignores blank, empty lines. It only increments the counter when a line contains actual text or characters.
How to Number Every Single Line (Including Blanks)
In programming and debugging, blank lines are just as important as text lines. If an error is on line 50, but the nl command skipped three blank lines earlier in the file, its numbering will be completely inaccurate relative to what the compiler sees.
To force the command to number all lines, regardless of whether they are empty, you must use the body numbering flag (-b) and set it to all (a).
nl -b a /var/log/syslog
Now, the output will perfectly mirror the strict line numbering used by text editors and compilers.
How to Customise the Number Formatting
If you are piping the output of this command into another program, or saving it to a log file, the default right-aligned numbers might look messy. You can change the number formatting using the -n (number format) flag.
To left-align the numbers without any padding, use ln (left justified, no leading zeros):
nl -n ln /var/log/syslog
To keep the numbers right-aligned but pad them with leading zeros (which is incredibly useful for sorting data alphabetically later), use rz (right justified, leading zeros):
nl -n rz /var/log/syslog
This will output numbers formatted as 000001, 000002, making it visually uniform and easy to parse programmatically.
Piping Data into nl
The true power of Linux utilities comes from chaining them together. You do not have to use nl on a static file. You can pipe the output of any other command into it.
For example, if you want to list all running processes and number them to see exactly how many are active, you can pipe the ps command directly into nl:
ps aux | nl -b a
This simple combination immediately turns a massive wall of text into an organised, numbered list.