• +4917626969472
  • info@ur-techpartner.de

Learn Basic Commands for LINUX systems – 04/04

Basic Linux for ROS Drone

Learn Basic Commands for LINUX systems – 04

The “.bashrc” file

Whenever a new Shell session is initialized (i.e. new command terminal is opened), Linux execute a special bash script which is named as “.bashrc” file. Linux System automatically generates it and always  place it in the HOME folder (on the path “/home/user”, or as in our case /home/kashif , where kashif is the user name). The “.bashrc” is a hidden file, however we can modify it to customize our terminal session.

Let’s open it with “gedit” command.

gedit .bashrc

The .”bashrc” file will be opened. It includes extensive lines of code. You can find all the configuration of terminal session here. For example you can find the color configuration of the command prompt in the file as follows

if [ "$color_prompt" = yes ]; then

    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

else

    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

fi

Lets change the color of the user displayed in terminal

Put following lines of code at the end of the “.bashrc” file, and save it.

export PS1="\${debian_chroot:+(\$debian_chroot)}\\[\\033[01;36m\\]\\u\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00m\\]\\\$ "

close the file (or open new terminal) to execute next command.

Run following command to execute “.bashrc” script

source .bashrc

You can see the color of the user name in command prompt is changed. Similarly, we can add commands into the “.bashrc” file that we want to be executed every time when the new terminal is opened.

Command “export”

In Linux system, we have environment variables which are part of the environment in which process runs. For example, TEMP, HOME, PS1 are the environment variables. These variables are set when a new terminal is opened. However, if we change the environment variable during the opened terminal, the change is not picked by the terminal. In this case, the “export” command updates the current terminal session. As we have seen before, we changed the variable PS1 and used “export” command so that the “export” command should also be executed and change can be applied/picked during current session.

If we simply write “export” command on terminal, it displays all environment variables.

export

As there are large number of environment variables, we can filter them out to view the necessary variable. For this grep command is used.

Command “grep”

This “grep” command is used to filter the elements.

Let’s write on terminal

export | grep ROS

We can see that first export command was executed to get environment variables, then the result was filtered to show only those variables which contain “ROS” characters.

The “grep” command is very useful, it cannot only be used with export command but also with other commands.

For example we use “grep” command along with “ls” command.

ls

Then use ls with grep

ls | grep ws

it displayed only those elements which contain “ws” in them.

“export” command can be used to set the variables by writing command on terminal.

export ROS_PACKAGE_PATH="/home/kashif/catkin_ws/src;”

 

Visualize Processes

The running processes can be viewed by using “ps” command along with “aux” argument

Write on terminal

ps aux

This command displays a number of processes. But you can filter these processes by using “grep” command.

Like

ps aux | grep my_test_process

 

Kill Process using Ctrl + C and “kill” command

You can kill process by using Ctrl + C command. If a process is running in a terminal and you want to close it. You should get focus on that particular terminal and press Ctrl + C, the process will be stopped and you will get back the control of command prompt to execute other commands.

“kill” command

Another command to kill the process is “kill” command. However, in order to use this command you will have to give process id (PID) like this

kill PID

You should know the process id (PID) of the process for using “kill” command. As we have already seen PID is displayed when “ps” command is used. Another command to view the process is “top” command. You can enter “top” command on terminal and the information about processes will be displayed.

kill 35

(Here PID is 35)

Secure Shell (ssh) Protocol

ssh protocol is used to connect to a remote machine. Using ssh protocol you can log into a remote machine from your local machine. It’s like a client server architecture, where local machine is a client and remote machine is a server. By login into remote machine you can transfer files and execute commands. This protocol is usually used to control the physical robot from a computer or a laptop.

Since we don’t have real robot attached with this computer, therefore we cannot practically do it at this time. However I will write the syntax of ssh protocol

The command structure is as follows

ssh <user>@<host>

where <host> will be the IP of the remote machine. You should also know the user name and password to login to the remote machine.

For example

ssh username@127.0.0.1

Sometimes, it may give port error. For example, port 22: connection refused.

For this you will have to give the “-p” argument, which is port.

To find out on what port ssh is running, Enter

ps aux | grep ssh

For example port is 3416

Write again

ssh username@127.0.0.1 -p 3416

 

Commands “sudo” and “apt”

Linux uses a packaging system which is a way to provide programs and applications for installation. Because of this we don’t need to build a program from a source code. Advance Package Tool (apt command) is used to interact with this packaging system. This command can be used to install, update, clean packages. After fresh installation of Linux system you need to run this command to update the database.

Enter in terminal

apt-get update

What happens, There is an error that permission is denied. This is normal, because we do not have permission to access packages database. For this we use “sudo” command. This command temporarily makes us super user. When we do not have permission as a normal user, we add “sudo” in command line to execute the required commands.

Enter in terminal

sudo apt-get update

You will see it worked. Similarly you can install different available packages like this.

sudo apt-get install <Package name>

Here we will end this Linux commands blogs. Hope you will be able to use Linux basic commands. You can continue exploring further useful command/features of Linux system.

  1. Asad Tariq

    I found LINUX commands blogs very useful and easy to understand.
    Thank You for sharing them

One comment