Using a terminal (demo)

Using a terminal (demo) #

The following demo is meant to (quickly) illustrate some benefits of a properly configured terminal, as well a few terminal-based applications.

This is not an introduction to bash or Linux core utilities.

Virtual environment #

For this demo, we deployed a virtual machine with a pre-configured terminal.

The instructions for connecting to this machine are identical to the one for the project evaluation environment.

Once you are connected to the virtual machine, you can follow the instructions below.

Warning. The demo below is not meant to be executed with your own terminal. In particular, some software is needed that may not be installed on your computer.

Creating a static website #

As an exercise, we will create a static website, using a simple framework called Hugo.

First, in our home folder, let us create a subfolder dedicated to our projects. For instance, we can call this subfolder workspace. To create this folder, open a terminal and run:

mkdir workspace

Then navigate to this folder with the command cd (you do not need to type the full name of the directory, just type cd w, and press the Tab key for autocompletion):

cd workspace

Next, we will (loosely) follow the quick start tutorial for Hugo. Explanations about some of the command below can be found here.

Copy-paste the following instruction and press Enter:

hugo new site mySite

This will create a project in a fresh folder called mySite. Let us navigate to it (again, you can take advantage of autocompletion):

cd mySite

Now copy-paste the following commands to declare this folder as a git repository and download a graphical theme for our website:

git init
git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke.git themes/ananke
echo "theme = 'ananke'" >> hugo.toml

At any moment, you can run the command:

pwd

to display the current directory. With this command, you can check that you are indeed in the folder mySite.

To get a overview of the content of the current folder, run:

ll

To get a recursive overview of the content of this folder, run:

tree

In order to navigate to a certain subfolder in this tree, we can take advantage of the fuzzy finder. For instance, let us assume that we want to move to some folder called socials, but we forgot its exact path.

Run:

c

This will display the paths to all directories in this tree. You can type characters to restrict your search, and use the up down arrow keys to select your destination. For instance, type the three character soc. The best matches are displayed at the bottom of the list. To select one of them, navigate up and down with the arrow keys and press Enter.

Note. Thanks to fuzzy search, the characters that you type do not need to be contiguous in a path. For instance, when you typed soc, all paths containing these three letters (in that order, but not necessarily contiguous) were retained.

Now let us navigate back to the mySite folder. Here we can use zoxide, which uses our path navigation history to allow faster navigation. Run:

zi

and then type (the beginning of) mySite.

Similarly, to go back to the socials subfolder that we previously went to, run:

zi

again and type (the beginning of) socials.

Alternatively, you can switch between your current location and the previous one (thanks to zoxide still) with:

zz

Starting the web server #

Navigate (back) to the mySite folder (you should know how to do this by now), and run:

hugo server

This will start a local development server for our website. To visualize the site, open Firefox (from the app menu), and use the address displayed in the terminal (it should be http://localhost:1313/)

To stop the server (or any Linux process running in a terminal), go back to the terminal an press Ctrl+C.

Next, we will add content to our website, while keeping the server running.

To restart the server, we can use our command history. Type the first letters of the command that we used above to start the serve (e.g. hu). By pressing the up and down arrow keys, you can scroll through the commands that you already typed and start with these letters. Scroll until you find the command hugo server, and press Enter to restart the server.

In order to keep the server running, we will execute our next command in another terminal. You can create a new one by clicking on the boxed “+” icon:

Adding content #

Navigate to the (immediate) subfolder content of mysite. Then create a subfolder named posts:

mkdir posts

Navigate to this new folder. Then create a new text file called myPost.md. You can for instance do this with the command:

gedit myPost.md

which will open a new file with this name in the text editor “gedit”.

Note. If you only type ge followed by Tab, the shell will suggest you a list of programs whose name starts with these letters. You can scroll through them by pressing Tab again, or add a letter to disambiguate your search.

In the text editor, copy-paste the following content:

+++
title = 'My First Post'
date = 2024-01-14T07:07:07+01:00
+++

## Introduction

This is **bold** text, and this is *emphasized* text.

### Subsection

This is a [link](https://gohugo.io) to the Hugo website.

In this file, everything below the header (title/date) is written in Markdown.

Save the file, and check with Firefox that your first post has been added to your website.

Warning. To see your changes, you may need to force Firefox to clear its cache: in “Settings | Privacy & Security | Cookies and Site Data”, click on the “Clear Data” button.

If this does not work, then you can stop and restart the Hugo server, as explained above.

Aliases #

The command c that we used above is an alias for a more complex command, namely cd $(find * -type d | fzf)

We declared this alias in the file ~/.zshrc (remember that ~ is a shortcut for your home folder).

Let us open this file to see the declaration of this alias. Navigate to your home folder, for instance with:

cd

And list its content:

ll

The file .zshrc should be there. Open this file with gedit:

gedit .zshrc

Note. Again, you can take advantage of autocompletion here. For instance, press ged followed by Tab followed by .z followed by Tab.

Towards the end of this file, you will see the declaration of the alias:

alias c='cd $(find * -type d | fzf)'

Let us create another useful alias. Ubuntu has a convenient command called xdg-open that opens a file with the default application associated to this file’s extension (this is the equivalent of a double-click in a graphical file browser). Let us set a simpler name than xdg-open to execute this command. For instance o (like “open”).

First, let us check that the name o is not used already for another command. Open a new terminal and run:

o

You should get a “command not found” message, which confirms that this command is free.

Now let us add the following line to the .zshrc file:

alias o='xdg-open'

Save the file and open a new terminal.

You can now use the o command to open any file.

To see this, let us navigate back to the socials folder that we were previously in (e.g. using zi, as explained above). If you list the files in this folder (with ll), then you will see that they have the .svg extension. To open one of these files, you can type o and the first letters of the name of the file, then Tab for autocompletion. This will open the file with the default image viewer application on this machine.

Scripts #

If you want to use an alias to execute a (possibly complex) sequence of commands, then the preferred way is to write a script.

For instance, we could write a script that navigates to the mySite folder and then starts the hugo server. And we may use the alias ws (like “website”) to call this script (you can check that it is free).

Navigate to the ~/bin folder, and create a text file called ws (for instance with gedit, as we did above for the post).

In this file, copy-paste the following:

#! /bin/bash

cd ~/workspace/mySite
hugo server start

Observe that these are the two instructions for the two tasks that we want to execute. Save the file, and make it executable by running:

sudo chmod u+x ws

To test your script, close all running instances of hugo (if any), e.g. with Ctrl + C, as explained above.

Now regardless of your location, you can type ws to start the web server.

Note. Linux shells (such as bash or zsh) support the same language for commands and scripts. This is a full-fledged programming language (with conditional statements, loops, etc.), where an instruction can also be a command.

Terminal-based applications #

To conclude the demo, we introduce a few convenient applications that run in a terminal. These may save you time (compared to similar applications that rely on a GUI).

ripgrep #

ripgrep allows you to search files that contain certain words or regular expressions (your IDE offers a similar functionality). For instance, let us assume that we want to search for all files under mySite that contain the string “Canada”.

Navigate to the mySite folder. Then run:

rg Canada

This will display the path to each (text) file that contains this word (in this example, there is only one match), and the corresponding line numbers.

ranger #

ranger is a popular terminal-based file browser.

To open it, type:

ranger

You can navigate within the current directory with the up and down arrow keys, and in the directory tree (from child to parent and conversely) with the left and right arrow keys. Note that it also displays previews of text files.

To quit ranger, you can press the letter q.

htop #

htop allows you to monitor processes running on your machine. To open it, type:

htop

The upper part of the interface displays memory and CPU usage, whereas the lower part displays running process (sorted by CPU usage by default). You can kill a process with F9.

To quit htop, you can press the letter q.

ncdu #

ncdu allows you to visualize the amount of disk space taken by your files and programs.

To open it, type:

ncdu

The folders and files in the current folder are sorted by disk space, and you can navigate the directory tree using the arrow keys (like with ranger).

To quit ranger, you can press the letter q.