From 520a7c7db1364eb92e6e613f690eb0ceda668c9e Mon Sep 17 00:00:00 2001 From: Jared Kobos Date: Fri, 2 Feb 2018 11:28:18 -0500 Subject: [PATCH] [UPDATE] View the Beginning of Text Files with head --- ...w-the-beginning-of-text-files-with-head.md | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/docs/tools-reference/tools/view-the-beginning-of-text-files-with-head.md b/docs/tools-reference/tools/view-the-beginning-of-text-files-with-head.md index 2d51ed5d3ae..7c4dae8bc1c 100644 --- a/docs/tools-reference/tools/view-the-beginning-of-text-files-with-head.md +++ b/docs/tools-reference/tools/view-the-beginning-of-text-files-with-head.md @@ -2,60 +2,57 @@ author: name: Linode email: docs@linode.com -description: Use the Linux command head to view the beginning of a text file -keywords: ["head", "linux", "common commands", "cli"] +description: 'Use the Linux command head to view the beginning of a text file' +og_description: 'Head is a Unix command line utility for viewing the beginning of text files. This guide shows how to use head and gives practical examples.' +keywords: ["head", "linux", "utilities", "cli", "text files"] license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)' aliases: ['linux-tools/common-commands/head/'] -modified: 2011-04-19 +modified: 2018-02-02 modified_by: name: Linode published: 2010-10-25 title: View the Beginning of Text Files with head --- -The `head` command is a core Linux utility used to view the very beginning of a text file. Despite its narrow functionality, `head` is useful in many systems administration and scripting tasks. For similar functionality that address the end of a file, consider the tail utility. +The `head` command is a core Linux utility used to view the very beginning of a text file. Despite its narrow functionality, `head` is useful in many systems administration and scripting tasks. For similar functionality that address the end of a file, use the [tail](/docs/tools-reference/tools/view-and-follow-the-end-of-text-files-with-tail/) utility instead. -![Title graphic](/docs/assets/view_the_beginning_of_text_files_with_head_smg.png) +![View Beginning of Files with Head](/docs/assets/view_the_beginning_of_text_files_with_head_smg.png) ## Using head -Consider the following invocation: +List the file or files you want to view after the `head` command: head /etc/rc.conf -This will print the first 10 lines of the `/etc/rc.conf` file to standard output on the terminal. Thus, `head` is useful for a number of different situations such as determining the contents of a file if the file names are ambiguous. +This will print the first 10 lines of `/etc/rc.conf` to standard output. If a file has fewer than 10 lines, `head` will print the entire file. -If a file has fewer than 10 lines, `head` will print the entire file. +### Control the Length of Output -### Control the Length of Output with head - -With the `-n` option, the number of lines that `head` outputs can be modified. For example: +With the `-n` option, the number of lines that `head` outputs can be modified: head -n 24 /etc/logrotate.conf -This prints the first 24 lines of the `/etc/logrotate.conf` file to the terminal. You can specify the number of lines before or after you declare the file. Therefore, the following command is equivalent to the previous command: +This prints the first 24 lines of `/etc/logrotate.conf` to the terminal. You can specify the number of lines before or after you declare the file: head /etc/logrotate.conf -n 24 If a file is smaller than the specified number of lines, `head` will print the entire file. -### View the Beginning of Multiple Files with head +### View Multiple Files -`head` can process multiple files at once. Consider the following: +`head` can process multiple files at once: -{{< output >}} -$ ls -example roster + head example.txt names.txt -$ head * -==> example <== +{{< output >}} +==> example.txt <== lollipop The Joke Jockey to the Fair Simon's Fancy Truckles -==> roster <== +==> names.txt <== John Susan Michael @@ -68,18 +65,24 @@ George Jacob {{< /output >}} -`head` outputs the first ten lines of each file by default. If you are using `head` to read more than one file, you may also use the `-n` option to control the number of lines printed. +To view the first line of every file in a directory, you can use the `-n` option combined with the `*` wild card: + + head -n 1 * + +### View Command Output -### Combine head with Other Commands +By using the pipe operator, `head` can be used to filter the output of commands as well as files: -`head` can be used to filter the output of commands as well as files. For instance: + cat --help | head -n 2 {{< output >}} -% cat --help | head -n 2 Usage: cat [OPTION]... [FILE]... Concatenate FILE(s), or standard input, to standard output. +{{< /output >}} + + ls /usr/lib | head -$ ls /usr/lib | head +{{< output >}} alsa-lib ao apr.exp @@ -91,5 +94,3 @@ avahi awk bmp {{< /output >}} - -In the first example, `head` filters the full output of `cat --help` to generate only the first two lines of the output of the command. In the second example, `head` prints the first ten lines of the output of the `ls` command.