Note
This section is optional, the ROS2 tutorial starts at ROS2 Installation.
Ubuntu Terminal Basics
You already know how to turn on your computer and press some keys to make bits flip and colorful pixels shine on your monitor. Here, we’ll go through a few tips on Ubuntu.
Note
The world is full of smart people, and they’ve done some amazing stuff, like Ubuntu and Linux. There are endless tutorials for those and this is not a complete one. In this section, we’ll go through some basic tools available in Ubuntu’s terminal that help with our quest to learn/use ROS2.
Who cares about the terminal anyways, are you like 100 years old or something?
Besides the unintended upside that if you’re typing into a terminal fast enough with a black hoodie, you’re cosplaying Mr. Robot at a very low cost, there wouldn’t be another way to make a tutorial like this within the current age of the Universe without relying on Ubuntu’s terminal.
GUIs change faster than long tutorials like this one can keep up with and terminal is our reliable partner in crime and unlikely to change much in the foreseeable future.
For the whole tutorial, you can copy and paste the commands in terminal. If it doesn’t work, it’s either your fault or mine, but surely not the terminal‘s.
The terminal
Note
Check out Canonical’s Tutorial on terminal for the complete story.
Hint
You can open a new terminal window by pressing CTRL+ALT+T.
Warning
This section is about the default terminal in Ubuntu 22.04. If you prefer to use some other terminal instead (there are many), then this might not be useful to you, and you might be happier referring to its documentation instead.
The terminal is one of those things with many names. Some call it shell, some console, some command line, some terminal. I’m sure there’s someone furiously typing right now saying that I’m wrong and describing in detail what those differences might be. The truth is that, in the wild (a.k.a. the Internet), those terms are used pretty much as synonyms.
For all intents and purposes, Tom Hanks is not stuck in this terminal. Instead, we use it to send commands to Ubuntu and make stuff happen.
Program |
Example usage |
What it does |
---|---|---|
pwd |
|
Outputs the absolute path to the current directory. |
mkdir |
|
Makes a directory called |
cd |
|
Changes directory to a specified target. |
touch |
|
Creates an empty file called |
cat |
|
Outputs into the console the contents of |
rm |
|
Removes a file or directory (with the |
ls |
|
Lists the contents of the current directory. |
grep |
|
Outputs the lines of |
nano |
|
Helps you edit a file using a (relatively?) user-friendly program so that you don’t get stuck into vim. |
sudo |
|
With the powers of a super user, do something. It allows a given user to modify sensitive files in Ubuntu. |
apt |
|
Installs Ubuntu packages, in this case, git. |
alias |
|
Creates an alias for a command, i.e. another way to refer to it. |
Let’s use it. (!?)
The thing is, we’ll be using the terminal throughout the entire tutorial, so don’t worry about going too deep right now.
To warm up, let’s start by creating an empty file inside a new directory, as follows
Hint
The path ~
stands for the currently logged-in user’s home folder.
Hint
You can open a new terminal window by pressing CTRL+ALT+T.
Warning
For copying from the terminal use CTRL+SHIFT+C. For pasting to the terminal, use CTRL+SHIFT+V. Be careful with CTRL+C, in particular. It is used to, in simple terms, close running programs on the terminal.
cd ~
mkdir a_folder
cd a_folder
touch an_empty_file.txt
Then, we can use nano to create another file with some contents
nano file_with_stuff.txt
Then, nano will run. At this point we can start typing, so let’s just type
stuff
then you can exit with the following keys
CTRL+X
Y
ENTER
you can also look at the bottom side of the window to know what keys to press. As an example, in nano, ^X
stands for CTRL+X.
Then, if you run
ls
the output will be
an_empty_file.txt file_with_stuff.txt
we can, for example, get the contents of file_with_stuff.txt
with
cat file_with_stuff.txt
whose output will be
stuff
So, enough of this example, let’s get rid of everything with
Warning
ALWAYS be careful when using rm. The files removed this way do NOT go to the trash can, if you use it you pretty much said bye bye bye to those files/directories.
cd ~
rm -r a_folder
bash redirections
Hint
Before defaulting to writing a 300-lines-long Python script for the simplest and most common of tasks, it is always good to check if there is something already available in bash that can do the same thing in an easier and more stable way.
In a time long long ago, before ChatGPT became the new Deep Magic, bash was already tilting heads and leaving Ubuntu users in awe.
Among many powerful features, the redirection operator, >
, stands out. It can be used to, unsurprisingly, redirect the output of a command to a file.
Warning
The operator >
overwrites the target file with the output of the preceding command, it does not ask for permission, it just goes and does it.
The operator >>
appends to the target file with the output of the preceding command.
Don’t mix these up, there is no way to undo.
For example, if we want to store the result of the command ls
to a file called result_of_ls.txt
, the following will do
cd ~
ls > result_of_ls.txt
As a default in this version of Ubuntu, if the file does not exist it is created.
Tab completion
Hint
Use TAB completion extensively.
Whenever I have to look at a novice’s shoulders while they interact with the terminal it gives me a certain level of anxiety. That is because they are trying to perfectly type even the longest and meanest paths for files, directories, and programs.
The terminal has TAB completion, so use it extensively. You can press TAB at any time to complete the name of a program, folder, file, or pretty much anything.
For example, we can move to a folder
cd ~
Then type a partial command or a part of its arguments. For example,
rm result_o
then, by pressing TAB, it should autocomplete to
rm result_of_ls.txt
Be careful with sudo
Warning
DO NOT, I repeat, DO NOT play around with sudo.
With great power, comes great opportunity to destroy your Ubuntu. It turns out that sudo is the master key of destruction, it will allow you to do basically anything in the system as far as the software is concerned.
So, don’t.
For these tutorials, only use sudo when installing system-wide packages. Otherwise, do not use it.
Be careful even when not using sudo
With regular user privileges, the major system folders will be protected from tampering. However, our home folder, e.g. /home/<YOU>
will not.
In our home folder, we are the lords, so a mistake can be fatal for your files/directories.
File permissions
Warning
DO NOT, I repeat, DO NOT play around with sudo, chmod, or chown.
One of the reasons that using sudo indiscriminately will destroy your Ubuntu is file permissions. For example, if you simply open a file and save it as sudo, you’ll change its permissions, and that might be enough to even block you from logging into Ubuntu via the GUI.
I will not get into detail here about programs to change permissions because we won’t need them extensively in these tutorials. However, it is important to be aware that this exists and might cause problems.
nautilus: browsing files with a GUI
To some extent similar to explorer in Windows and finder in macOS, nautilus is the default file manager in Ubuntu.
One tip is that it can be opened from the terminal as well, so that you don’t have to find whatever folder you are again. For example,
Hint
The path .
means the current folder.
cd ~
nautilus .
will open the currently logged-in user’s home folder in nautilus.