December 23, 2024

Linux Command Line Tutorial for Robotics, Control, and Machine Learning/Data Science Engineers – Part 1

In this first part of the Linux and Ubuntu command line tutorial for robotics, mechatronics, control, and machine learning engineers, we explain

  • How to start a terminal, create folders, and files.
  • How to edit files and list the content of folders (directories).
  • How to create hidden files, erase files, and erase folders.
  • What are root and home directories in Linux.

Here is the main motivation for creating this tutorial. Learning the Linux command line is very important if you want to become a robotics, control, or machine learning engineer. For example in Robotics, the Linux command line is very important since Robot Operating System (ROS) is based on the Linux command line. We have noticed that a significant number of students and robotics learners are simply copying and pasting different Linux commands from online tutorials without properly understanding the meaning of the commands that they are typing. This is a very ineffective learning practice. Moreover, this is a dangerous practice that should be avoided since it can create issues with an operating system or it can cause the removal of important files. Consequently, it is of paramount importance to properly learn how to use the Linux command line. The YouTube video accompanying this webpage tutorial is given below.

First, let us learn how to open a terminal in Linux/Ubuntu. Click on Activities in the top left corner of the screen, and in the activated search menu type “terminal” or “shell”. Another approach for opening a terminal in Ubuntu Linux (as well as in some other Linux distributions) is to press Ctrl-Alt-T. The terminal window will appear.

Next, we need to learn how to determine our current absolute path with respect to the root directory (folder). In the terminal, type

pwd

The output is the absolute path with respect to the root directory. The command “pwd” is an abbreviation for Print Working Directory. The forward slash symbol “/” denotes the root directory. This is the root that stores all the directories and files in the Linux system. To see your username, you need to type

whoami

Let us now go to the home folder. We can do that by typing

cd ~

The command “cd” is short for “change directory”. We will talk about this command later on. The symbol “~ is a shortcut for the home folder (directory) of the current user. To list the content of the current directory, we use the command

ls -l

The option -l will list the directory structure of the current folder in the long format. However, this command will not list the hidden files or hidden folders. To show the hidden files and folders, you need to type

ls -la

The hidden files in the linux system usually start with a dot “.” We do not need to go to the folder in order to list its content. We can do that by typing

ls -la “absolute path to the folder”

For example, to list the content of the folder “etc” that is located in the root directory “/”, we need to type

ls -la /etc

Here, the forward slash symbol “/” is an abbreviation of our “root” directory.

To create a directory, we use the command “mkdir” which is short for “make directory”. Let us create a folder called “test1”. To do that, let us type

mkdir test1

Currently, we are in the home directory, and to navigate to the new folder, we need to use the “cd” command:

cd test1

Let us now type

pwd

to see our current location. Our current location is “/home/aleksandar/test1”. Let us go back to the home folder. To go back to the parent folder of the current folder, we need to use “cd” with a double period. That is,

cd ..

will bring us back from the test1 folder to the home folder.

We can create a folder and a subfolder in the folder by typing

mkdir -p ~/test2/subfolder2

This command will create a folder called “test2” and inside of that folder, it will create another folder called subfolder2. Let us confirm this by investigating the structure of the folder called “test2”. We can do that by typing

cd test2
ls -l

To create empty files, we need to use a command called  “touch”. For example, to create an empty text file, we type

touch empty_text.txt

To create an empty hidden file, we type

touch .empty_text_hidden.txt

Note that the file name starts with a period. This means that the file is hidden. If you type

ls -l

You will only be able to see the file “empty_text.txt” and not the hidden file “.empty_text_hidden.txt”. To see all the files including the hidden files, we need to type

ls -la

Let us learn how to edit files. To edit the file, we will use “gedit” editor. To install “gedit”, we type

sudo apt-get install gedit

Then, type the following in order to edit the file “empty_text.txt

gedit  empty_text.txt

After this command, geditor will open, and you will be able to edit the file. Similarly, we can edit the hidden file by typing

gedit .empty_text_hidden.txt

and you will be able to edit the file.

Next, let us learn how to erase files and folders. First, while we are still in “test1” folder let us create an additional file

touch new_file.txt

To list the content of the directory, type

ls -la

To erase a file, we need to use a command called “rm” (short for “remove”). To delete the file we type

rm new_file.txt

Next, let us learn to erase all the files, while keeping the directory. First, let us create another folder inside of the folder test1, and let us navigate to the new folder

mkdir subfolder1
cd subfolder1

Then, let us create two files

touch file1.txt
touch file2.txt
ls -l

To erase both files at the same time, we type rm with a star symbol

rm *
ls -l

after executing these two commands you can see that the folder is empty. The star symbol “*” is used in Linux to denote that we want to perform the operation on all the files in the folder. That is, it stands for everything. This is a very powerful symbol that can be used to perform very elegant operations. We will thoroughly explain this symbol in our future tutorials. Next, let us learn how to completely erase the folder and its content. To do that, let us go back to the home folder by typing

cd ~

To erase the folder “test1” and all its content including files and subfolders, we type

rm -r test1

The parameter “-r” means recursively erase all the content of the folder. If you now type

ls -l

You will notice that the folder “test1” does not exist. Also, let us erase the created folder “test2” and its content. To do that we type

cd ~
rm -r test2