Linux Basics (Units 1-5)

Unit 1 - The Terminal

A terminal is the common name for the program that does two main things. It allows you to type input to the computer (i.e. run programs, move/view files etc.) and it allows you to see output from those programs. All Unix machines will have a terminal program available.

Open the terminal application. You should now see something that looks like the following:

Terminal application

There will be many situations where it will be useful to have multiple terminals open and it will be a matter of preference as to whether you want to have multiple windows, or one window with multiple tabs (there are typically keyboard shortcuts for switching between windows, or moving between tabs).


Unit 2 - Your first Unix command

It’s important to note that you will always be inside a single directory when using the terminal. This is called your working directory. When you open a new terminal you start in your own home directory (containing files and directories that only you can modify). To see what files and directories are in our home directory, we need to use the ls command. This command lists the contents of a directory. If we run the ls command we should see something like:

ubuntu@:~$ ls
command_line_course  linux_bootcamp
ubuntu@:~$  

There are four things that you should note here:

  1. You will probably see different output to what is shown here, it depends on your computer setup. Don’t worry about that for now.
  2. The ubuntu@:~$ text that you see is the Unix command prompt. In this case, it contains a user name (‘ubuntu’) and the name of the current directory (‘~’, more on that later). Note that the command prompt might not look the same on different Unix systems. In this case, the $ sign marks the end of the prompt.
  3. The output of the ls command lists two things. In this case, they are both directories, but they could also be files. We’ll learn how to tell them apart later on. These directories were created as part of a specific course that used this bootcamp material. You may see something different on your own computer.
  4. After the ls command finishes it produces a new command prompt, ready for you to type your next command.

The ls command is used to list the contents of any directory, not necessarily the one that you are currently in. Try the following:

ubuntu@:~$ ls /home/ubuntu/tutorial_materials/Documentation
Mac keyboard shortcuts.pdf  Unix and Perl.pdf

ubuntu@:~$ ls /etc/perl
CPAN  Net

Unit 3 - The Unix tree

Looking at directories from within a Unix terminal can often seem confusing. But bear in mind that these directories are exactly the same type of folders that you can see if you use any graphical file browser. From the root level (/) there are usually a dozen or so directories. You can treat the root directory like any other, e.g. you can list its contents:

ubuntu@:~$ ls /
bin   etc         lib         media  proc  sbin  sys  var
boot  home        lib64       mnt    root  snap  tmp  vmlinuz
dev   initrd.img  lost+found  opt    run   srv   usr

You might notice some of these names appearing in different colors. Many Unix systems will display files and directories differently by default. Other colors may be used for special types of files. When you log in to a computer you are working with your files in your home directory, and this is often inside a directory called ‘users’ or ‘home’.


Unit 4 - Finding out where you are

There may be many hundreds of directories on any Unix machine, so how do you know which one you are in? The command pwd will Print the Working Directory and that’s pretty much all this command does:

ubuntu@:~$ pwd
/home/ubuntu

When you log in to a Unix computer, you are typically placed into your home directory. In this example, after we log in, we are placed in a directory called ‘ubuntu’ which itself is a subdirectory of another directory called ‘home’. Note that this can be extremely confusing at first. Remember that on a Unix system multiple users might be logged into a system. Every user account has a home directory and they are all stored in /home.

Conversely, ‘ubuntu’ is the parent directory of ‘tutorial_materials’. The first forward slash that appears in a list of directory names always refers to the top level directory of the file system (known as the root directory). The remaining forward slash (between ‘home’ and ‘ubuntu’) delimits the various parts of the directory hierarchy. If you ever get ‘lost’ in Unix, remember the pwd command.

As you learn Unix you will frequently type commands that don’t seem to work. Most of the time this will be because you are in the wrong directory, so it’s a really good habit to get used to running the pwd command a lot.

There is another way to know where you are on most Unix systems. The command prompt usually tells you exactly where you are in the file system. It even tells you the machine you are logged into. Look at the example below:

ubuntu@my-machine:~$ cd tutorial_materials/
ubuntu@my-machine:~/tutorial_materials$ ls
Applications  Code  Data  Documentation
ubuntu@my-machine:~/tutorial_materials$  

Here you can see that we are user ubuntu logged into my-machine. Hopefully that is fairly intuitive. The colon is used to separate the machine name from your current location on the machine. The tilde (~) is used as a shorthand to indicate your home directory. In other words (~) is the same as /home/ubuntu.

In the above machine we can see what happens when we change from the ubuntu user home directory (/home/ubuntu) to the directory tutorial_materials. Note that the prompt changes and indicates where we are in the file system (i.e. ~/tutorial_materials is the same as /home/ubuntu/tutorial_materials).

Let’s see what happens to the prompt to if we move to the root directory:

ubuntu@my-machine:~/tutorial_materials$ cd /
ubuntu@my-machine:/$ ls
bin   etc         lib         media  proc  sbin  sys  var
boot  home        lib64       mnt    root  snap  tmp  vmlinuz
dev   initrd.img  lost+found  opt    run   srv   usr
ubuntu@my-machine:/$ cd /home
ubuntu@my-machine:/home$ ls
adminuser  ubuntu
ubuntu@my-machine:/home$  

Note that the tilde (~) disappears and is replaced by the full path to the directory we are in. In the example above this is / (the root of the filesystem tree) and then changes to the /home directory as we move into it.


Unit 5 - Making new directories

Let’s change back into the ubuntu user’s home directory (/home/ubuntu)

ubuntu@:/home$: cd /home/ubuntu

If we want to make a new directory (e.g. to store some work related data), we can use the mkdir command:

ubuntu@:~$ ls
Desktop    Downloads  Music     Public     tutorial_materials
Documents  media      Pictures  Templates  Videos
ubuntu@:~$ mkdir Learning_unix
ubuntu@:~$ ls
Desktop    Downloads      media  Pictures  Templates           Videos
Documents  Learning_unix  Music  Public    tutorial_materials
ubuntu@:~$