Introduction to the command line IV

So, how is everyone's new year's resolution? :-)

Before I delve into the specifics of scheduling repeat tasks with cron, let us first have a closer look at a few other basic commands for editing and filtering text that are often featured in beginner tutorials, and the ways to combine them in bash scripts.

Remember when we discussed directory listing using the command ls and its various modifications? You can combine it with the awk command to only display one of the columns:

ls -la | awk '{print $5}'

This comes in handy in system maintenance scripts.

You can also, for instance, go to your Documents folder and list all the file and directory names with upper case letters switched to lower case, like this

ls | tr 'A-Z' 'a-z'

Or replace all the spaces with an underline, like this

ls | tr ' ' '_'

Snippets like these can be used in scripts for batch-renaming files.

As you can imagine, tr is a very powerful command, you will often encounter it in more complex scripts that cut the text out in various ways, modify it, and stitch all the pieces back together. I will get back to this later on.

How about extracting a single line of text to use it as input for another command or to insert it into another document?

We can call on sed:

sed -n -e 7p linux_article_x.txt

The snippet above displays only the line number 7 from one of my blog post drafts in the Terminal.

The most frequent use of sed is a very crude and straightforward search-and-replace mechanism:

sed 's/http/https/g' index.html > index2.html && mv index2.html index.html

The example above replaces all the instances of http with https in the given file. Emphasis on _all_. Use with caution.

If, for some reason, I wanted to completely remove HTML markup from a file, and only keep plain text, I could use dehtmldiff:

dehtmldiff index.html > index.txt

Last but not least, whenever you feel your .bash_history file is getting too long, you can type

history -c

in the command line followed by

history -w

Next time you open your favourite Terminal application you will encounter the proverbial clean slate.

Scheduling repeat tasks that I'm going to introduce in the following post is not only useful for setting up simple reminders to yourselves (there are other applications aimed at non-expert users that might be better fit for this purpose), you can use it to automate all sorts of maintenance tasks from synchronising the contents of a folder between computers to keeping track of all the changes to your PhD thesis or other important personal writing project.

You can, for instance, set up regular backups of your writing by adding this line to one of your scripts:

tar -cvf thesis_$(date +%y%m%d).tar /full/path/to/your/thesis

Or for frequent syncing between computers via USB, you can use rsync and place this snippet into your script

rsync -av /your/writing/project/ /path/to/USB/

Works both ways, by the way, just switch the paths.

Next time: cron plus more script snippets. I'll try and do my best to have the continuation ready by the end of February.

copy left Johanka Piskovská, 2020