What is Linux command to view first line of file?

0227

Viewing the First Line of a File in Linux

In the Linux operating system, there are several commands you can use to view the first line of a file. The most common and straightforward command is head.

Using the head Command

The head command is used to display the first few lines of a file. By default, it will display the first 10 lines of the file. To view only the first line, you can use the -n 1 option.

Here's the basic syntax for using the head command to view the first line of a file:

head -n 1 <filename>

For example, if you have a file named example.txt, you can use the following command to view its first line:

head -n 1 example.txt

This will output the first line of the example.txt file.

graph LR A[Linux Terminal] --> B[head command] B --> C[View first line of file] C --> D[Output first line]

Other Commands for Viewing the First Line

While the head command is the most common way to view the first line of a file, there are a few other commands you can use:

  1. cat: The cat command can be used to display the entire contents of a file, including the first line. To view only the first line, you can use the following command:

    cat -n1 <filename>

    This will output the first line of the file, along with the line number.

  2. sed: The sed command can be used to extract specific lines from a file. To view the first line, you can use the following command:

    sed -n 1p <filename>

    This will output the first line of the file.

  3. awk: The awk command is a powerful text processing tool that can be used to manipulate and extract data from files. To view the first line of a file, you can use the following command:

    awk 'NR==1' <filename>

    This will output the first line of the file.

All of these commands can be useful in different situations, depending on your specific needs and the complexity of the task at hand. The head command is generally the most straightforward and commonly used option for viewing the first line of a file in Linux.

0 Comments

no data
Be the first to share your comment!