Linux Commands – head
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed less command in Linux which is used for the pagination of a large file.
https://cloudaffaire.com/linux-commands-less/
In this blog post, we will discuss head command in Linux. head command is used to print some lines from the top of the file and output to standard output. By default, head command prints the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. You can also use head command with pipe.
Linux Commands – head:
You can use head command to print some lines form the top of a file and output to standard output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
########################### ## Linux Commands | head ## ########################### ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##----- ## head ##----- ## head [options]... [file] ## create a file with 100 lines > myfile; i=1; while [ $i -le 100 ]; \ do echo "$i" >> myfile; ((i++)); done ## create some additional files cp myfile myfile1 cp myfile myfile2 head myfile ## returns top 10 lines head myfile myfile1 myfile2 ## returns top 10 lines of each ## files along with filename as header ps -ax | head ## also works with pipe |
You can use head -n K or –lines=K options to output the first K lines of a file or pipe. However, if K starts with a ‘-‘, head command prints all but the last K lines of each file.
1 2 3 4 5 6 7 8 |
## head -n or --lines=[-]NUM option head myfile ## returns 1st 10 lines head -n 20 myfile ## returns 1st 20 lines head -n -20 myfile ## returns all lines except ## bottom 20 lines |
You can use head -c K or –bytes=K options to print the first K bytes, instead of initial lines. However, if K starts with a ‘-‘, head prints all but the last K bytes of each file. K maybe an integer optionally followed by, one of the following multiplicative suffixes:
- ‘b’ => 512 (“blocks”)
- ‘KB’ => 1000 (KiloBytes)
- ‘K’ => 1024 (KibiBytes)
- ‘MB’ => 1000*1000 (MegaBytes)
- ‘M’ => 1024*1024 (MebiBytes)
- ‘GB’ => 1000*1000*1000 (GigaBytes)
- ‘G’ => 1024*1024*1024 (GibiBytes)
1 2 3 4 5 6 7 8 |
## head -c or --bytes=[-]NUM options ls -l myfile ## myfile1 is 141 bytes long head -c 50 myfile ## returns 1st 50 bytes of data head -c -50 myfile ## returns all file data except ## bottom 50 bytes of data |
You can use head -v or –verbose options to always print file name headers. By default, head prints the file name header only if multiple files were provided as input.
1 2 3 4 5 6 7 |
## head -v or --verbose options head myfile ## filename is not printed as header head -v myfile ## filename is printed as header head myfile myfile1 myfile2 ## filenames are printed as header |
You can use head -q or –quiet or –silent options to never print file name headers. This is the default behavior of head command if a single file is provided as input.
1 2 3 4 5 6 7 |
## head -q or --quiet or --silent head myfile myfile1 myfile2 ## filenames are printed as header head -q myfile myfile1 myfile2 ## filenames are not printed as header rm my* |
Hope you have enjoyed this article. In the next blog post, we will discuss tail command in Linux.