blog.johanka.net

Introduction to the command line (head, less, tail, diff)

Today we are going to have a look at a few more basic commands, as well as the ways to combine them when manipulating text files on Linux.

Head and tail both work very similarly, and are quite intuitive as well. If, for some reason, you only need the first line (or lines) of a text file you can type

head -n 1 file.txt

in the command line prompt. Analogically, if you type in

tail -n 3 file.txt

only the last three lines of said file get printed on the screen. Now, what use can you have with these two commands?

Supposing you wanted to create an index (or a table of contents) for your blog. You could go to the individual files, copy the first lines and paste them to a file named index. Or you could use the command line tools and type in this

head -n 1 blog/01.txt >> blog/index.txt
head -n 1 blog/02.txt >> blog/index.txt

and so on. I will show you the ways to make this even more compact later on.

Similarly, if you have the same code or footer text at the bottom of all pages on your website, you can copy it to the new blogposts like this

tail -n 3 blog/04.txt >> blog/05.txt

Commands less and more can also be used for filtering text. Their feature sets and usage partially overlap with what I have already shown you, so I will not go into greater detail in this case.

Let us now have a look at another useful and very simple command, diff. Again, if you are me and have several instances of the same text in differing stages of completion scattered throughout multiple files on your computer, you don't have to place all the various versions side by side to see how they differ, you can use the command diff. Very simple and straightforward. Just type in

diff file1 edited_version_of_file1

(supposing they are both in the same folder) and the system will output only the lines that differ, including the line numbers.

Now, when I'm finished writing all this, I'm going to add all the necessary formatting (so you don't just see a huge bulk of unstructured typing next time you visit your favourite blog :-) at johanka.net), save the text file, copy (cp original_location/file new_location/) it to the folder where the other blog posts reside and type this in the Terminal

head -n 5 l*_en_07.php > linux_en_08.php && cat l*08.txt >> l*_en_08.php && tail -n 15 l*_en_07.php >> l*_en_08.php

Here I instructed the system to take the first lines in the file named linux_en_07.php, paste them into a newly created file with the name linux_en_08.php, copied the text of this blog post into it and added the last 15 lines of the previous one to the end of it.

I will then check if there's anything missing, make corrections when necessary, close the file, delete all the duplicates I don't need any more, and send all the updated documents to the web server so you can see them.

I had to fix a couple awkward turns of phrase as well as one serious blunder in the last post of this blog series. From now on, I will not attempt more than one 600-word article per week in both languages. Less is indeed more.

copy left Johanka Piskovská, 2019