/# The Linux Terminal
In this exercise, we will familiarize ourselves with the Linux terminal.
Starting the Terminal¶
To open the terminal, click on the terminal icon:
Create a second terminal window, either by:
- Right-clicking on the terminal and selecting the “Open Terminal” or
- Selecting “Open Terminal” from the “File” menu
Create a second terminal within the same window by pressing “Ctrl+Shift+T” while the terminal window is selected.
Close the 2nd terminal tab, either by:
- clicking the small ‘x’ in the terminal tab (not the main terminal window)
- typing
exit
and hitting enter.
The window will have a single line, which looks like this:
ros-industrial@ros-i-kinetic-vm:~$
This is called the prompt, where you enter commands. The prompt, by default, provides three pieces of information:
- ros-industrial is the login name of the user you are running as.
- ros-i-kinetic-vm is the host name of the computer.
- ~ is the directory in which the terminal is currently in. (More on this later).
Close the terminal window by typing
exit
or clicking on the red ‘x’ in the window’s titlebar.
Altering Files¶
mv Command¶
- Type
mv test.txt test2.txt
, followed byls
.- You will notice that the file has been renamed to
test2.txt
.
This step shows howmv
can rename files.
- You will notice that the file has been renamed to
- Type
mv test2.txt new
, thenls
.- The file will no longer be present in the folder.
- Type
cd new
, thenls
.- You will see
test2.txt
in the folder.
These steps show howmv
can move files.
- You will see
- Type
mv test2.txt ../test.txt
, thenls
.test2.txt
will no longer be there.
- Type
cd ..
, thenls
.- You will notice that
test.txt
is present again.
This shows howmv
can move and rename files in one step.
- You will notice that
cp Command¶
- Type
cp test.txt new/test2.txt
, thenls new
.- You will see
test2.txt
is now in thenew
folder.
- You will see
- Type
cp test.txt "test copy.txt"
, thenls -l
.- You will see that
test.txt
has been copied totest copy.txt
.
Note that the quotation marks are necessary when spaces or other special characters are included in the file name.
- You will see that
rm Command¶
- Type
rm "test copy.txt"
, thenls -l
.- You will notice that
test copy.txt
is no longer there.
- You will notice that
mkdir Command¶
- Type
mkdir new2
, thenls
.- You will see there is a new folder
new2
.
- You will see there is a new folder
You can use the -i
flag with cp
, mv
, and rm
commands to prompt you when a file will be overwritten or removed.
Job management¶
Stopping Jobs¶
- Type
./sample_job
.- The program will start running.
- Press Control+C.
- The program should exit.
- Type
./sample_job sigterm
.- The program will start running.
- Press Control+C.
- This time the program will not die.
Stopping “Out of Control” Jobs¶
- Open a new terminal window.
- Type
ps ax
. - Scroll up until you find
python ./sample_job sigterm
.- This is the job that is running in the first window.
- The first field in the table is the ID of the process (use
man ps
to learn more about the other fields).
- Type
ps ax | grep sample
.- You will notice that only a few lines are returned.
- This is useful if you want to find a particular process
- Note: this is an advanced technique called “piping”, where the output of one program is passed into the input of the next. This is beyond the scope of this class, but is useful to learn if you intend to use the terminal extensively.
- Type
kill <id>
, where<id>
is the job number you found with theps ax
. - In the first window, type
./sample_job sigterm sigkill
.- The program will start running.
- In the second window, type
ps ax | grep sample
to get the id of the process. - Type
kill <id>
.- This time, the process will not die.
- Type
kill -SIGKILL <id>
.- This time the process will exit.
Showing Process and Memory usage¶
- In a terminal, type
top
.- A table will be shown, updated once per second, showing all of the processes on the system, as well as the overall CPU and memory usage.
- Press the Shift+P key.
- This will sort processes by CPU utilization.
This can be used to determine which processes are using too much CPU time.
- This will sort processes by CPU utilization.
- Press the Shift+M key.
- This will sort processes by memory utilization
This can be used to determine which processes are using too much memory.
- This will sort processes by memory utilization
- Press q or Ctrl+C to exit the program.
Editing Text (and Other GUI Commands)¶
- Type
gedit test.txt
.- You will notice that a new text editor window will open, and
test.txt
will be loaded. - The terminal will not come back with a prompt until the window is closed.
- You will notice that a new text editor window will open, and
- There are two ways around this limitation. Try both…
- Starting the program and immediately returning a prompt:
- Type
gedit test.txt &
.- The
&
character tells the terminal to run this command in “the background”, meaning the prompt will return immediately.
- The
- Close the window, then type
ls
.- In addition to showing the files, the terminal will notify you that
gedit
has finished.
- In addition to showing the files, the terminal will notify you that
- Type
- Moving an already running program into the background:
- Type
gedit test.txt
.- The window should open, and the terminal should not have a prompt waiting.
- In the terminal window, press Ctrl+Z.
- The terminal will indicate that
gedit
has stopped, and a prompt will appear.
- The terminal will indicate that
- Try to use the
gedit
window.- Because it is paused, the window will not run.
- Type
bg
in the terminal.- The
gedit
window can now run.
- The
- Close the
gedit
window, and typels
in the terminal window.- As before, the terminal window will indicate that
gedit
is finished.
- As before, the terminal window will indicate that
- Type
Running Commands as Root¶
- In a terminal, type
ls -a /root
.- The terminal will indicate that you cannot read the folder
/root
. - Many times you will need to run a command that cannot be done as an ordinary user, and must be done as the “super user”
- The terminal will indicate that you cannot read the folder
- To run the previous command as root, add
sudo
to the beginning of the command.- In this instance, type
sudo ls -a /root
instead. - The terminal will request your password (in this case,
rosindustrial
) in order to proceed. - Once you enter the password, you should see the contents of the
/root
directory.
- In this instance, type
Warning: sudo
is a powerful tool which doesn’t provide any sanity checks on what you ask it to do, so be VERY careful in using it.