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:
Let's look at some examples of using these commands.
>> pwd
/var/mail
The current working directory is mail, within the var directory. Forward slashes /
are used to indicate different directories.
>> 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 -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).
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.
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:
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 -
symbol is used to denote permissions that are not available.
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.
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>
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>