Terminals and You

Terminals

Terminals are text based methods of interacting with a computer. They can be used to interact with your local machine or a machine over the network. They are how we'll run Python. Let's dive in.

Commands

There are many different terminal commands. The usual base format for a command is:

<command> -<flags/options>

Possibly the most useful command is man which can be used to open manuals for commands. For example:

man man

opens up the man manual. Throughout this section we'll explore various commands.

At some point you've probably navigated through your computers files and folders/directories using a file explorer. You can navigate through directories much the same way in a terminal. These are the commands you'll need for navigation:

  • pwd : Prints the current working directory, which is the directory the terminal is currently "in".
  • ls : Lists the contents (files and directories) of the current directory.
  • cd : Used to change the current directory.

Let's look at some examples of using these commands.

pwd

>> pwd
/var

The current working directory is var.

>> pwd
/var/mail

The current working directory is mail, within the var directory. Forward slashes / are used to indicate different directories.

cd

>> cd Dropbox

Move to the Dropbox directory.

>> cd Documents/Notes

Move to the Notes directory within the Documents directory.

>> cd /

Move to the root directory. This is a special directory which all other directories are under, either directly or indirectly.

>> cd ~/

Move into the current users User directory.

>> cd ..

Move back one directory.

ls

>> ls
emails  Notes.txt run

The current directory has three items in it:

  • emails
  • Notes.txt
  • run

ls -l

>> ls -l
drw-rwxr-x  2 root mail  4096 Sep 14  2012 emails
-rw-r--r--  1 root root    78 Sep 14 00:05 Notes.txt
lrwxrwxrwx  1 root root     4 Sep 14  2012 run -> /run

Running ls with the -l option gives much more information about the contents of the current directory. Let's interpret the first entry going from left to right in order:

drw-rwxr-x  2 root mail  4096 Sep 14  2012 emails
  • The first character d in drw-rwxr-x indicates that emails is a directory. The remaining rw-rwxr-x are the permissions on the directory (more on this later).

  • 2 indicates the number of hard links to the emails directory (not that important for our purposes).

  • root is the user that owns the directory (more on this later).
  • mail is the group the directory belongs to (more on this later).
  • 4096 is the size of the directory link in bytes.

  • Sep 14 2012 is the last time the directory was modified.

  • emails is the directory name.

Looking at

-rw-r--r-- 1 root root 78 Sep 14 00:05 Notes.txt

The initial - where there was a d before indicates that Notes.txt is a file. The permissions on the file are also different.

Looking at

lrwxrwxrwx 1 root root 4 Sep 14 2012 run -> /run

The initial l where there was a d before indicates that the entry is a link. In this case, trying to cd into run would take us to /run.

Users, Groups and Permissions

A user corresponds to a user of a computer. For example, the username you use to log into your machine is an example of a user. Computers can have many user identities available.

Users can belong to many different groups, which are used to give them permission to perform operations on files and directories.

When looking at permissions there are three commons letters to be familiar with:

  • r: read (needed to read the file).
  • w: write (needed to write to the file).
  • x: execute (needed to run the file as as script).

The set of permissions

rw-rwxr-x

is easier to think about if you chunk it into sets of three.

Owner | Group | Everyone
rwx   | r-x   | r--

Each chunk indicates what different users are allowed to do with the file. In this case

  • The owner user can read, write and execute the file.
  • Users belonging to the group can read and execute the file.
  • Everyone else on the machine can only read the file.

The - symbol is used to denote permissions that are not available.

Changing Permissions

Sometimes you want to change the permissions on a file, either to restrict access to the file or be able to run the file as a script. This task can be accomplished using the chmod command.

  • chmod u+x <file>: Allow the owner to execute the file.
  • chmod g+w <file>: Allow group members to write to the file.
  • chmod g-x <file>: Remove execute permission for group members.

When using chmod you may sometimes have to prepend sudo in front of it to gain the permission needed to modify the file permissions.

Sudo

The sudo command temporarily lets you act like the root user. root usually has very broad access to the machine and in general its better to avoid using sudo if you can.

Path

The echo command can be used to display text.

>> echo hello
hello

It useful for viewing the values of variables in the terminal. One of these variables is the PATH. Variables can be echoed out by appending a \$ in front of them.

>> echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

The PATH variable contains a list of directories in which the terminal will look for commands. Directories are separated by :. The directories in the above PATH are:

  • /usr/local/sbin
  • /usr/local/bin
  • /usr/sbin
  • /usr/bin

When a command is entered, the terminal will look in each of these directories in order to find the corresponding program to execute. The which command can be used to determine where a command is being pulled from. For example

>> which python
/usr/bin/python

shows us that python is being pulled from the /usr/bin directory.

You can add directories to PATH by running:

export PATH=$PATH:<NEW PATH>

SSH

The ssh command is used to log into remote machines. You'll probably need to use it at some point to log into the Physics Departments machines. This will look like

ssh <user>@ssh.phas.ubc.ca

The general format is

ssh <user>@<address>

CP and SCP

The cp command is used to copy files.

cp <source> <target>

This will copy the source file to the target file. scp is a version of this that can be used to copy files over a network to a remote machine, say from your computer onto the department server

scp <source> <user>@ssh.phas.ubc.ca:<path to target>

or from the department server to your computer

scp <user>@ssh.phas.ubc.ca:<path to source> <target>