Bash View Start (head)

Display the first lines of files

๐Ÿ“„ What is the head Command?

The head command displays the first lines of a file. By default, it shows the first 10 lines, making it ideal for previewing file contents or checking headers without opening the entire file.


# View first 10 lines
head document.txt
                                    

Head Features

๐Ÿ“‹

First Lines

View beginning of files

head file.txt
๐Ÿ”ข

Custom Count

Specify number of lines

head -n 5 file.txt
๐Ÿ“

Multiple Files

View several files at once

head file1.txt file2.txt
๐Ÿ’พ

Byte Count

Display by bytes not lines

head -c 100 file.txt

๐Ÿ”น Basic Usage

The tail command is a core utility for viewing the end portion of files, particularly useful for logs and large text files. Its default behavior, invoked simply as tail filename, is to output the last 10 lines to the terminal. This provides a quick, resource-efficient way to inspect the most recent entries without loading the entire file. It is indispensable for system administrators checking the latest system log entries (/var/log/syslog), developers verifying the end of application output, or anyone needing a snapshot of recent data. The command reads input from files or standard input, making it highly versatile in pipelines.

# Create a sample file
seq 1 20 > numbers.txt

# View first 10 lines
head numbers.txt

Output:

1
2
3
4
5
6
7
8
9
10

๐Ÿ”น Specify Number of Lines

You can control exactly how many lines tail outputs using the -n (number) option followed by a count. The command tail -n 20 file.log displays the last 20 lines. A common shorthand is to omit the -n and use just a hyphen followed by the number (e.g., tail -50 file.log). You can also specify a starting point with a plus sign: tail -n +11 file.log outputs everything from line 11 to the end, effectively skipping the first 10 lines. This precision is key for extracting specific sections from files, such as omitting headers or isolating recent error blocks in diagnostics.

# View first 5 lines
head -n 5 numbers.txt

# Shorthand notation
head -5 numbers.txt

# View first 3 lines
head -n 3 numbers.txt

Output (first 5 lines):

1
2
3
4
5

๐Ÿ”น View Multiple Files

Tail can simultaneously monitor the ends of several files, which is invaluable for comparative log analysis. When you specify multiple filenames (e.g., tail -f /var/log/auth.log /var/log/syslog), tail outputs a header line like ==> /var/log/auth.log <== before each file's content. This clear visual separation allows you to watch events unfold across different system or application logs in a single terminal window. Combining this with the -f flag creates a powerful live dashboard for servers or applications that write to multiple log files, helping correlate events and errors across different services in real-time.

# Create sample files
echo -e "File 1\nLine A\nLine B" > file1.txt
echo -e "File 2\nLine X\nLine Y" > file2.txt

# View both files
head file1.txt file2.txt

Output:

==> file1.txt <==
File 1
Line A
Line B

==> file2.txt <==
File 2
Line X
Line Y

๐Ÿ”น Display by Bytes

The -c option changes head's unit from lines to bytes, outputting the first N bytes of input. This is critical for non-text files, such as checking the magic number (file signature) of a binary, peeking at the header of an image or archive, or ensuring a file is not empty. It's also used in network programming or when dealing with raw data streams where the concept of a "line" is irrelevant, and precise byte-count control is required.

# View first 20 bytes
head -c 20 file.txt

# View first 100 bytes
head -c 100 document.txt

Output (first 20 bytes):

1
2
3
4
5
6
7
8
9

๐Ÿ”น Combining with Other Commands

head is frequently placed at the end of pipelines to limit the volume of output from previous commands. For instance, dmesg | tail -50 | head -10 would show lines 41-50 of the kernel ring buffer. This practice prevents terminal flooding during exploration, allows for safe testing of commands that might produce enormous output, and enables sampling of data streams. It embodies the Unix principle of connecting small tools to manage data flow effectively.

# Show first 5 files in directory
ls -l | head -n 5

# View first 3 processes
ps aux | head -n 3

# Get top 10 largest files
du -h * | sort -hr | head -n 10

Example Output:

total 48
-rw-r--r-- 1 user group 1234 Jan 10 file1.txt
-rw-r--r-- 1 user group 5678 Jan 10 file2.txt

๐Ÿ”น Useful Head Options

Key options like -n, -c, and -q (quietโ€”suppress headers) provide the versatility needed for both interactive and scripted use. In scripts, -q ensures clean output without decorative headers. In data analysis, alternating between -n and -c allows inspection based on logical records or physical size. Understanding these options lets you wield head not just as a preview tool, but as a precise data extraction and flow control component in complex shell pipelines.

Common Options:

  • -n N - Display first N lines
  • -c N - Display first N bytes
  • -q - Quiet mode, suppress file headers
  • -v - Verbose mode, always show headers
  • -n -N - Show all except last N lines
# Show all except last 5 lines
head -n -5 file.txt

# Quiet mode (no headers)
head -q file1.txt file2.txt

# Always show headers
head -v file.txt

๐Ÿ”น Practical Examples

Everyday uses of head include validating data files, monitoring log heads, and creating data samples. A data scientist might run head -1000 large_dataset.csv > sample.csv to create a manageable sample for testing scripts. A developer might use head -20 application.log to check recent activity after a deployment. These scenarios highlight head's role as a gatekeeper, providing a safe, controlled way to interact with the beginning of files and streams, which is often where the most current or defining information resides.

# Check CSV file headers
head -n 1 data.csv

# Preview log file
head -n 20 /var/log/syslog

# View first line of multiple files
head -n 1 *.txt

# Extract first paragraph
head -n 5 article.txt

๐Ÿง  Test Your Knowledge

How many lines does head show by default?