Using Linux commands

Developing ROS systems requires a certain level of familiarity with the basic processes of the Linux operating system (OS) and its file system.

The interaction with the Linux OS is enabled by the shell program using a Command Line Interface (CLI) called Terminal. The terminal enables the user to input the commands and the arguments for the commands and receive the feedback as the commands are executed.

The terminal can be started using keyboard shortcut Ctrl + Alt + T.

The commands are called in the terminal by writing the command name and pressing the Enter key. We often have to specify options and inputs for the commands which is done by writing them after the command name separated by spaces.

An example of a Linux command in the terminal:

rosuser$ echo "Hello ROS"
Hello ROS

Linux filesystem

Every file in Linux is contained in it's parednt directory, also called folder. The directories in the Linux OS are organized in a tree-like structure, where every directory is containted in its parent directory except the topmost directory called root.

Paths

The location of the file or directory in the filesystem is represented by the path, which is a sequence of all directories leading from the root directory to the current file/directory in the filesystem separated by the forward slash (/) character.

A path starting with / is an absolute path which means it represents the path from the root directory to the location of the file or directory.

A path without the leading / is called relative path representing the path from the current location in the filesystem to the specified file od directory.

A path can also start with the tilde (~) character which is a shorthand for the absolute path from the root directory to the home directory of the user.

Examples:

An example of absolute path:

/home/rosuser/Desktop

An example of relative path:

Desktop

An example of an absolute path with shorthand for the user 'rosuser' home directory (same as the absolute path above):

~/Desktop

Linux filesystem navigation and management

To inspect, modify and navigate the linux filesystem we can use linux commands in the terminal.

Display current location

The command 'pwd' displays the path to the current directory / location in the filesystem. By default, when openning a new terminal in linux the current location, also called current/present working directory, is the home folder of the user.

Example:

$ pwd
/home/rosuser

Change working directory

The current location can be changed using the 'cd' command (for change directory) and specifying the path to the new location. The path can be relative or absolute.

Example:

$ cd ~/ros_ws

# cd <path/to/new/location>

List all folders and files in a directory

To view all files and directories in the specified directory use the 'ls' command. Without passing the path to the directory, the contents of the current directory is listed. The command has many options for formatting the output, all options can be viewed using man ls or ls --help.

Example:

$ ls ~/ros_ws
build devel src

# ls <options> <path/to/directory>

Create new directory

A new directory is created using the 'mkdir' command. The command requires specifying the name of the new directory. Example:

$ mkdir -v rosin_ws
mkdir: created directory 'rosin_ws'

# mkdir <option> <new directory name>

Create new file

New file without contents in the current location can be created using 'touch' command and passing the name of the file.

Example:

$ touch my_robot_controller.cpp

# touch <option> <file name>
# option -v is for verbose

Environmental variables

Every time a shell session is started (by opening a terminal), there exists a list of variables that store the information that influences the shell's behaviour and access to resources. The variables can also be defined by the user.

The list of all the environmental variables and their values can be printed using the command printenv or env. One of the variables, for example, is USER. The value of the variable can be displayed with printenv USER or filtered from the variables list with env | grep USER.

Among the environmental variables are also some that are important for working with ROS. To view the variables with "ROS" as part of the name we will use:

env | grep ROS

You can read more on ROS environmental variables in the ros wiki, however, during this course we will only mention a few of them:

  • ROS_MASTER_URI holds the location of the master which is espacially important to set correctly when running ROS on multiple machines
  • ROS_PACKAGE_PATH is a list of locations where ROS tools search for ROS packages.

The values of the variables can be set and changed using the export command.