0

cat without -n

user@linux:~$ cat /etc/sysctl.conf | head -4
#
# /etc/sysctl.conf - Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables.
# See sysctl.conf (5) for information.
user@linux:~$ 

There is -n option in cat command to display the line number of the file.

user@linux:~$ cat -n /etc/sysctl.conf | head -4
     1  #
     2  # /etc/sysctl.conf - Configuration file for setting system variables
     3  # See /etc/sysctl.d/ for additional system variables.
     4  # See sysctl.conf (5) for information.
user@linux:~$ 

user@linux:~$ cat -n /etc/sysctl.conf | tail -4
    74  # Debian kernels have both set to 1 (restricted) 
    75  # See https://www.kernel.org/doc/Documentation/sysctl/fs.txt
    76  #fs.protected_hardlinks=0
    77  #fs.protected_symlinks=0
user@linux:~$ 

What about head and tail command? Is there any option to view the line number in head and tail without using cat -n and pipe the output to head or tail?

I've tried head -n <filename> and tail -n <filename> but no line number displayed on the output.

user@linux:~$ head -n4 /etc/sysctl.conf
#
# /etc/sysctl.conf - Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables.
# See sysctl.conf (5) for information.
user@linux:~$ 

user@linux:~$ tail -n4 /etc/sysctl.conf
# Debian kernels have both set to 1 (restricted) 
# See https://www.kernel.org/doc/Documentation/sysctl/fs.txt
#fs.protected_hardlinks=0
#fs.protected_symlinks=0
user@linux:~$ 
  • -n is the option for selecting the number of lines that you'd want head or tail to return. – Kusalananda May 28 at 17:22
2

What about head and tail command? Is there any option to view the line number in head and tail without using cat -n and pipe the output to head or tail?

No; the current versions of head and tail do not have any way to add line numbers to the output themselves.

  • 3
    There's nothing stopping the user from using head file | cat -n though. – Kusalananda May 28 at 17:21
  • 2
    Yes, I was afraid I was being too brief; I was responding directly to the "can head or tail number lines directly?" – Jeff Schaller May 28 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.