1. The Linux terminal
If you use Linux, you will notice that tasks are commonly carried out through a command-line interface (also called a terminal or shell). Familiarise yourself with the terminal using the following guides:
1.1. Tips
On linux, you can perform a quick copy paste by selecting any text (e.g. in a terminal, text editors etc), then middle click to directly paste without needing to use the keyboard.
Terminals can autocomplete text, so you only need to type a few letters and press tab to autocomplete. If there are multiple options press tab twice to see the options. You can also use autocomplete with the ipython terminal.
Use https://explainshell.com to explain the command line arguments of a specified linux command.
Explanation of command line arguments for the ls -lrt terminal command.
1.2. Commonly used commands
Command |
Description |
|---|---|
~ |
home directory |
cd ~ |
change to your home directory |
cd .. |
change to the directory above the current directory e.g. |
pwd |
list files in current directory |
program_name & |
sends application to the background |
ls |
list contents of current directory |
ls -lrt |
list contents of current directory with more details |
cat filename |
print contents of a text file to screen |
1.3. Environmental variables
In linux, environmental variables are used to store global variables.
Variable |
Description |
|---|---|
$PATH |
Identifies the directories that the shell searches in to find programs that you want to execute. This is represented as a single string, with directories separated by a colon e.g “/usr/bin:/usr/local/bin” |
$PYTHONPATH |
Identifies the directories that python searches in when importing modules. Individual directories are also separated by a colon. |
Note that these environmental variables are typically already set. To see what their value has been set to, use the echo command:
echo $PATH
By default, your $PATH variable could include the following values:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/snap/bin
Environmental variables can be set using the bash export command:
export PATH=/hpc/hpat721/usr/pycharm/pycharm-community-2019.3.3/bin/:$PATH
1.4. Adding program locations to your PATH environmental variable
You can add the directory where you have installed a program to your path so that you do not have type in the full path to the program to execute it. This can be achieved by adding the directory containing your program to the $PATH environmental variable.
For a Bash shell, you can set environment variables as shown e.g.
export PYTHONPATH=”/my/new/path/”
or you can prepend values to an existing environment variable e.g.
export PATH=”/my/new/path/:$PATH”
or you can append values to an existing environment variable e.g.
export PATH=”$PATH:/my/new/path/”
1.5. Editing terminal start up scripts
Each time a terminal is opened, a startup shell script is automatically run. You can store custom terminal commands in this startup shell script e.g. you could add directories which contain your programs to the PATH environment variable (described in the previous section) so that you can just run the program by typing its name in the terminal and not have to type in the full path.
The Bash shell startup script is located in the ~/.bashrc text file. For more information on bashrc files, see the following bashrc guide.
To edit the script, use the following command to start a text editor e.g. gedit
gedit ~/.bashrc
New environmental variables can be specified in this file or, alternatively, values can be appended to existing environmental variables.
After editing the file, save and exit. The updated path will then be available when you open a new terminal. If you want the PATH variable to be updated in your current terminal session, then type the following command:
source ~/.bashrc
1.6. Identifying running processes
Use the top command to list the running programs (also known as a process). Each running process has an associated process id (PID).
Output of top command.
Press the c button to see the full path to the program. To quit the top process viewer, press the q button.
Alternatively you can list processes that have been created by a specific user (e.g. user psam012 as shown below):
top -u psam012
If you know the name of a running command/process, you can directly search for it’s process id using the following command:
ps -ef | grep search_term
Where the search_term could be for example pycharm.sh. The process id is shown in the first column.
1.7. Killing processes
You can kill a process manually using the following command:
kill -9 process_id
With process_id being replaced by the process id number identified from the top or ps -ef commands.