Daily Linux Command: head

Do you need to memorize Linux commands? Daily Linux Command: man Daily Linux Command: cat head(1) is one of the most frequently used commands for file processing, and its function is to read the beginning part of a file.When you only need to get the beginning content of a file, head(1) is safer than cat(1) — it does not load the entire file, which is especially advantageous when dealing with large files.✦•———-Basic Usage———-•✦By default, head(1) reads the first 10 lines of a file.

$ head /etc/services# Network services, Internet style## Updated from https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml .## New ports will be added on request if they have been officially assigned# by IANA and used in the real-world or are needed by a debian package.# If you need a huge list of used numbers please install the nmap package.
tcpmux    1/tcp                         # TCP port service multiplexerecho      7/tcp

You can specify the number of lines to display using the -n parameter (this parameter is also common in other similar commands, such as tail(1), which also uses -n to control the number of lines).

$ head -n 5 /etc/services# Network services, Internet style## Updated from https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml .## New ports will be added on request if they have been officially assigned

In addition to controlling by lines, you can also control the length of the read by bytes using the -c parameter:

$ head -c 22 /etc/services# Network services, In

✦•———-Advanced Usage Tips———-•✦

  • Check if a compressed package contains a top-level folder

head(1) has a very practical trick: preview whether a compressed package contains a top-level directory before extracting.

You may have encountered this problem — some compressed packages have a top-level directory, while others do not. If you manually create a directory and then extract, the compressed package with a directory structure will have an extra layer after extraction; while the package without a directory will scatter files everywhere, making it very messy.

With head(1), we can quickly preview whether a compressed package contains a top-level directory without extracting the entire file, ensuring the accuracy of the extraction path and improving efficiency.

$ tar -tf Packages/V_9_8_P1.tar.gz | headopenssh-portable-V_9_8_P1/openssh-portable-V_9_8_P1/.dependopenssh-portable-V_9_8_P1/.git_allowed_signersopenssh-portable-V_9_8_P1/.git_allowed_signers.ascopenssh-portable-V_9_8_P1/.github/openssh-portable-V_9_8_P1/.github/ci-status.mdopenssh-portable-V_9_8_P1/.github/configsopenssh-portable-V_9_8_P1/.github/configure.shopenssh-portable-V_9_8_P1/.github/run_test.shopenssh-portable-V_9_8_P1/.github/setup_ci.sh

In the example above, we used a pipe (|) to take the output of tar(1) as the input for head(1), reading just the first 10 lines is enough to determine whether the compressed package contains a top-level directory.

  • In-depth discussion: Why doesn’t head(1) need to read the entire compressed package?

The principle is not complicated.

When using a pipe to connect two commands (like tar … | head), they run simultaneously. The pipe has a buffer (usually a few KB to tens of KB). tar(1) gradually decompresses and outputs the file list, writing each line to the pipe, while head(1) reads data from the pipe, and once it has read the specified number of lines (default 10 lines), it exits.

When head exits, the writing end of the pipe (i.e., the tar command) receives a SIGPIPE signal when it tries to continue writing, which by default terminates the process. Therefore, tar also stops and does not continue to decompress the entire file.

Congratulations, you have gained useful knowledge again✨

Leave a Comment