How to display a specific number of lines?

0113

To display a specific number of lines from a file in Linux, you can use the head or tail commands with the -n option. Here’s how to do it with both commands:

Using head

The head command is used to display the beginning of a file. To specify the number of lines, use the -n option followed by the desired number.

Example:

head -n 5 filename.txt

This command will display the first 5 lines of filename.txt.

Using tail

The tail command is used to display the end of a file. Similarly, you can specify the number of lines with the -n option.

Example:

tail -n 10 filename.txt

This command will show the last 10 lines of filename.txt.

Summary

  • head -n X filename.txt: Displays the first X lines of the file.
  • tail -n Y filename.txt: Displays the last Y lines of the file.

These commands are particularly useful for quickly checking the contents of large files without opening them entirely. If you have any more questions or need further examples, feel free to ask!

0 Comments

no data
Be the first to share your comment!