Add Line Numbers to “cat” Output

cat can be used to print the contents of a file in a Terminal, and it’s often useful to number each line of the output (including empty lines).

Adding line numbers is as simple as adding the -n switch to the command:

cat -n filename.txt

Example Output, with -n switch:

$ cat -n filename.txt

1 Line 1
2 Line 2
3 Line 3
4 Line 4
5 Line 5

Example Output, without -n:

$ cat filename.txt

Line 1
Line 2
Line 3
Line 4
Line 5