10. head command - muneeb-mbytes/linux_course GitHub Wiki

head _command

head command is a command-line utility, which prints the first 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name.

Untitled Diagram drawio

      Fig-1: All variations of head command

Cheat sheet of head command

Sr.no head commands description
1 head prints only the first 10 lines of the file
2 head -c [file_name] it prints the number of characters or bytes as mentined
3 head -n [file_name] show the specified number of lines
4 head -q [file_name] don't separate the content of multiple files with a file name tag
5 head -v [file_name] show the file name tag
6 head file1 file2 prints the first 10 lines of each file.
7 head --help Gives all possible variations available in head

head [file _name]

In the below Fig-2 its showing that printing the first 10 lines defaultly when we use head file _name.

Untitled Diagram drawio (4)

      Fig-2: head[file_name]

The below GIF shows the whole process from sourcing scripting file till the output.

gif-head (2)

     GIF-1: giphy for head[file_name]

Github link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/head_commands/head.csh

head -c

The 'head -c' command counts the number of bytes/characters of a file.

syntax

head -c [number] [file _name]

In this Fig-3 it shows the number of bytes or characters which we mentioned .Here each character is considered as one byte and it also considers the spaces and lines as the bytes. so here we used the head -c 60 file_name it considered only 60 characters including spaces and lines too.

Untitled Diagram drawio

      Fig-3: head -c <num> [file_name]

The below GIF shows the whole process from sourcing scripting file till the output.

gif-byte

      GIF-2: giphy for head -c <num> [file_name]

Github link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/head_commands/head_bytes.csh

head -n

The 'head -n' option displays specified number of lines.

syntax

head -n [number] [file _name]

In the below Fig-4 we are considering the numbers of lines to be printed as we mentioned. Here i considered head -n8 file_name its prints the only first 8 lines of the file.

Here head -n file _name (or) head -n file _name same.

Untitled Diagram drawio (5)

      Fig-4: head -n <num> [file_name]

The below GIF shows the whole process from sourcing scripting file till the output.

gif-lines

      GIF-3: giphy for Fig-4: head -n <num> [file_name]

Github link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/head_commands/head_lines.csh

head -v

To display the file name before output we use verbose (-v)

syntax

head -v [file _name]

In the below Fig-5 we can see the contents with the file _name.Here we have considered head -v file _name command it prints the first 10 lines of the file with the file name.

Untitled Diagram drawio (2)

      Fig-5: head -v file_name

The below GIF shows the whole process from sourcing scripting file till the output.

gif-verbose (3)

      GIF-4: giphy for head -v file_name

Github link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/head_commands/head_verbose.csh

head -q

Display the content without the filename.

In Fig-6 we can see the contents without the file name .Here we have considered the 2 files and we used the head -q file1 file2 command it gives us the contents without file name regardlessly as one file.

Untitled Diagram drawio (6)

      Fig-6: head -q [file1] [file2]

The below GIF shows the whole process from sourcing scripting file till the output.

gif-quiet

      GIF-5: giphy for head -q [file1] [file2]

Github link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/head_commands/head_quiet.csh

head [file1] [file2]

This command prints the first 10 lines of each file.

In Fig-7 we can see that first 10 lines of each file wih file names.Here we have used the head [file1_name] [file2_name] command so it prints only first 10 lines of the each file.

Untitled Diagram drawio (7)

      Fig-7: head [file1_name] [file2_name]

The below GIF shows the whole process from sourcing scripting file till the output.

gif-multiple

      GIF-6: giphy for head [file1_name] [file2_name]

GitHub link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/head_commands/head_mutliple.csh

head --help

This command is used to display all possible variations of head command.

In the below Fig-8 you can see that by using this command we can see all possible variations of head command.

head_help

      Fig-8: head --help command

To learn tail command : https://github.com/muneeb-mbytes/linux_course/wiki/11.-tail-command

⚠️ **GitHub.com Fallback** ⚠️