What other commands use line numbering?

QuestionsQuestions8 SkillsProDec, 17 2025
0143

Great follow-up question! Beyond cat -n and grep -n, here are a few other common Linux commands that can display line numbers or have specialized line numbering features:

  1. nl (number lines): This is a dedicated command for numbering lines. It offers more advanced options for formatting line numbers, like numbering only non-empty lines, inserting a specific separator, or starting the count from a different number.

    nl /tmp/hello

    You'll notice it formats the numbers slightly differently than cat -n.

  2. less: When viewing files with less, you can enable line numbers interactively by pressing G (Shift + g) to go to the end of the file, then typing -N to toggle line numbers on or off. You can also start less directly with line numbers using the -N option:

    less -N /tmp/hello
  3. more: Similar to less, more is another command for viewing files paginated. While more doesn't have a direct command-line option for line numbers like less -N, some older versions or specific configurations might show them, or you might need to combine it with nl.

  4. vi/vim: These powerful text editors can display line numbers. Inside vi or vim, you can type :set nu (for "number") and press Enter to show line numbers. To hide them, use :set nonu. You can also use :set rnu for "relative number" which shows line numbers relative to the cursor position.

  5. awk: awk is a very versatile text processing language. You can easily add line numbers to its output. The built-in variable NR represents the current record (line) number.

    awk '{print NR, $0}' /tmp/hello

    This command prints the line number (NR) followed by the entire line ($0).

Each of these commands provides line numbering in a slightly different way or for a different purpose, showing the flexibility of Linux tools!

Is there anything else you'd like to explore about them?

0 Comments

no data
Be the first to share your comment!