{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Table of Contents\n", "* [1. Tutorial on ``head`` and ``tail``](#1.-Tutorial-on-head-and-tail)\n", "\t* [1.1 ``head``](#1.1-head)\n", "\t* [1.2 ``tail``](#1.2-tail)\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "export LANGUAGE=en # Be sure the commands output English text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Tutorial on ``head`` and ``tail``" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This short tutorial will show you how to use the linux command line tools ``head`` and ``tail``, to respectively print the first lines and last lines of a file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.1 ``head``" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first reflex is to print the help (option ``--help``) :" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Usage: head [OPTION]... [FILE]...\r\n", "Print the first 10 lines of each FILE to standard output.\r\n", "With more than one FILE, precede each with a header giving the file name.\r\n", "With no FILE, or when FILE is -, read standard input.\r\n", "\r\n", "Mandatory arguments to long options are mandatory for short options too.\r\n", " -c, --bytes=[-]K print the first K bytes of each file;\r\n", " with the leading '-', print all but the last\r\n", " K bytes of each file\r\n", " -n, --lines=[-]K print the first K lines instead of the first 10;\r\n", " with the leading '-', print all but the last\r\n", " K lines of each file\r\n", " -q, --quiet, --silent never print headers giving file names\r\n", " -v, --verbose always print headers giving file names\r\n", " --help display this help and exit\r\n", " --version output version information and exit\r\n", "\r\n", "K may have a multiplier suffix:\r\n", "b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\r\n", "GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\r\n", "\r\n", "GNU coreutils online help: \r\n", "Report head translation bugs to \r\n", "Full documentation at: \r\n", "or available locally via: info '(coreutils) head invocation'\r\n" ] } ], "source": [ "head --help" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will create a dummy file with random content, from the dictionnary (``/etc/dictionaries-common/words``):" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "cat /etc/dictionaries-common/words > /tmp/file.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can print the first 10 lines of this file ``/tmp/file.txt``:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\r\n", "A's\r\n", "AA's\r\n", "AB's\r\n", "ABM's\r\n", "AC's\r\n", "ACTH's\r\n", "AI's\r\n", "AIDS's\r\n", "AM's\r\n" ] } ], "source": [ "head /tmp/file.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we want the first 20 lines, we use the option ``-n NUMBER``:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\r\n", "A's\r\n", "AA's\r\n", "AB's\r\n", "ABM's\r\n", "AC's\r\n", "ACTH's\r\n", "AI's\r\n", "AIDS's\r\n", "AM's\r\n", "AOL\r\n", "AOL's\r\n", "ASCII's\r\n", "ASL's\r\n", "ATM's\r\n", "ATP's\r\n", "AWOL's\r\n", "AZ's\r\n", "AZT's\r\n", "Aachen\r\n" ] } ], "source": [ "head -n 20 /tmp/file.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's count how many lines there is:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "99171 /tmp/file.txt\r\n" ] } ], "source": [ "wc -l /tmp/file.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's a lot! Imagine we want to see all the file except the last 100 lines, we can use ``head`` with a negative value for ``NUMBER`` in the ``-n NUMBER`` option:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\r\n", "99071\r\n" ] } ], "source": [ "head -n 100 /tmp/file.txt | wc -l # 100 lines, from 1st to 100th\n", "head -n -100 /tmp/file.txt | wc -l # All lines but the last 100 lines, from 1st to 99071th" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.2 ``tail``" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The other command, ``tail``, works exactly like ``head`` except the lines are counted from the end:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Usage: tail [OPTION]... [FILE]...\r\n", "Print the last 10 lines of each FILE to standard output.\r\n", "With more than one FILE, precede each with a header giving the file name.\r\n", "With no FILE, or when FILE is -, read standard input.\r\n", "\r\n", "Mandatory arguments to long options are mandatory for short options too.\r\n", " -c, --bytes=K output the last K bytes; or use -c +K to output\r\n", " bytes starting with the Kth of each file\r\n", " -f, --follow[={name|descriptor}]\r\n", " output appended data as the file grows;\r\n", " an absent option argument means 'descriptor'\r\n", " -F same as --follow=name --retry\r\n", " -n, --lines=K output the last K lines, instead of the last 10;\r\n", " or use -n +K to output starting with the Kth\r\n", " --max-unchanged-stats=N\r\n", " with --follow=name, reopen a FILE which has not\r\n", " changed size after N (default 5) iterations\r\n", " to see if it has been unlinked or renamed\r\n", " (this is the usual case of rotated log files);\r\n", " with inotify, this option is rarely useful\r\n", " --pid=PID with -f, terminate after process ID, PID dies\r\n", " -q, --quiet, --silent never output headers giving file names\r\n", " --retry keep trying to open a file if it is inaccessible\r\n", " -s, --sleep-interval=N with -f, sleep for approximately N seconds\r\n", " (default 1.0) between iterations;\r\n", " with inotify and --pid=P, check process P at\r\n", " least once every N seconds\r\n", " -v, --verbose always output headers giving file names\r\n", " --help display this help and exit\r\n", " --version output version information and exit\r\n", "\r\n", "If the first character of K (the number of bytes or lines) is a '+',\r\n", "print beginning with the Kth item from the start of each file, otherwise,\r\n", "print the last K items in the file. K may have a multiplier suffix:\r\n", "b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\r\n", "GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\r\n", "\r\n", "With --follow (-f), tail defaults to following the file descriptor, which\r\n", "means that even if a tail'ed file is renamed, tail will continue to track\r\n", "its end. This default behavior is not desirable when you really want to\r\n", "track the actual name of the file, not the file descriptor (e.g., log\r\n", "rotation). Use --follow=name in that case. That causes tail to track the\r\n", "named file in a way that accommodates renaming, removal and creation.\r\n", "\r\n", "GNU coreutils online help: \r\n", "Report tail translation bugs to \r\n", "Full documentation at: \r\n", "or available locally via: info '(coreutils) tail invocation'\r\n" ] } ], "source": [ "tail --help" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "élan's\r\n", "émigré\r\n", "émigré's\r\n", "émigrés\r\n", "épée\r\n", "épée's\r\n", "épées\r\n", "étude\r\n", "étude's\r\n", "études\r\n" ] } ], "source": [ "tail /tmp/file.txt # Last 10 lines" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zygote\r\n", "zygote's\r\n", "zygotes\r\n", "Ångström\r\n", "éclair\r\n", "éclair's\r\n", "éclairs\r\n", "éclat\r\n", "éclat's\r\n", "élan\r\n", "élan's\r\n", "émigré\r\n", "émigré's\r\n", "émigrés\r\n", "épée\r\n", "épée's\r\n", "épées\r\n", "étude\r\n", "étude's\r\n", "études\r\n" ] } ], "source": [ "tail -n 20 /tmp/file.txt # Last 20 lines" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The option ``-n NUMBER`` has the same behavior, except that it uses ``-n +NUMBER`` to ask for all lines but the first ``NUMBER`` (where ``head`` was asking ``-n -NUMBER``) :" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\r\n", "99071\r\n" ] } ], "source": [ "tail -n 100 /tmp/file.txt | wc -l # 100 lines, from 99071th to 99171th\n", "tail -n +101 /tmp/file.txt | wc -l # All lines from line 101, from 101th to 99171th" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*That's all for today, folks!*" ] } ], "metadata": { "kernelspec": { "display_name": "Bash", "language": "bash", "name": "bash" }, "language_info": { "codemirror_mode": "shell", "file_extension": ".sh", "mimetype": "text/x-sh", "name": "bash" } }, "nbformat": 4, "nbformat_minor": 0 }