Skip to main content
Engineering LibreTexts

05-C.6.1: Working with Files and Directories - head/tail Commands

  • Page ID
    32146
  • The head Command

    The head command, as the name implies, prints lines at the top of the file; by default, it prints the first 10 lines of the specified files. There is an option that allows you to print a number of lines other than 10. If more than one file name is provided then data from each file is preceded by its file name.

    Syntax:

    head [OPTION]... [FILE]...
    

    Command Options

    Options Meaning
    -c, --bytes=,[-]NUM/ Print the first NUM bytes of each file; if the leading '-' is specified then print all but the last NUM bytes of each file.
    -n, --lines=,[-]NUM/ Print the first NUM lines instead of the first 10; if the leading '-' is specified then print all but the last NUM lines of each file.
    -q, --quiet, --silent Never print headers giving file names.
    -v, --verbose Always print headers giving file names.
    -z, --zero-terminated Line delimiter is NUL, not newline.

    There are a variety of options...but head is very useful for any of the options. The example show the top 10 lines since there is NO option specified. The second command specifies only the first five lines of the file are to be output.

    Linux terminal showing the head command. The head command, by default, prints the first 10 lines of a file. This value can be changed by using the -n command and providing the number of lines you desire to display
    Figure \(\PageIndex{1}\): The head Command. ("head command" by Patrick McClanahan is licensed under CC BY-SA 4.0)

    The tail Command

    The tail command is the complementary of head command.The tail command, as the name implies, prints the last N number of data of the given input. By default it prints the last 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.

    Syntax:

    tail [OPTION]... [FILE]...
    

    Command Options

    Options Meaning
    -c, --bytes=,[-]NUM/ Output the last NUM bytes; or use -c +NUM to output starting with byte NUM of each fileprint the first NUM bytes of each file; if the leading '-' is specified then print all but the last NUM bytes of each file.
    -n, --lines=,[-]NUM/ Output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM.
    -f, --follow
     
    Output appended data as the file grows.
    -q, --quiet, --silent Never print headers giving file names.
    -v, --verbose Always print headers giving file names.
    -z, --zero-terminated Line delimiter is NUL, not newline.

    The tail command operates just like the head command, except it operates from the bottom of the file, not the top.

    Adapted from:
    "Head command in Linux with examples" by Akash Gupta, Geeks for Geeks is licensed under CC BY-SA 4.0
    "Tail command in Linux with examples" by Akash Gupta, Geeks for Geeks is licensed under CC BY-SA 4.0