User Tools

Site Tools


2. Using the command line

When you log in via SSH, you are presented with the command line interface, by means of the shell. The current default is Bash. Bash stands for “Bourne Again SHell”, and is one (but the most commonly used by far) of many shells available for Linux. A shell is what interprets what you type into the prompt and makes things happen - different shells do things differently. Your default shell is bash, although you can change it if you wish, but this document will only give you a brief introduction to using the shell to run programs and use the filesystem.

The Prompt

The first thing you should notice is that your shell puts something at the start of every line. This is called the prompt and it looks something like this:


The default prompt tells us our username (tcmal), the host we're on (sontaran), and the path we're at (~, which is an alias for your home directory - /home/<username>).

We can enter commands after the prompt, hit enter, and once they're done executing we'll get another prompt back.

tcmal@sontaran:~$ whoami
tcmal@sontaran:~$ hostname -f
tcmal@sontaran:~$ pwd

While you're at your prompt, you can also use the arrow keys to scroll back up your command history, and Ctrl+R to search through it.

Moving around

As mentioned above, our current directory is shown at the start of our prompt. You'll almost always start off in ~.

We can use cd to change the directory we're in. It takes the name of the directory we want to change to, with two special cases - . always means stay in the same directory and .. means go one level up.

tcmal@sontaran:~$ cd ..
tcmal@sontaran:/home$ cd tcmal

This is more interesting if we make some directories, so let's make a few test ones using mkdir:

tcmal@sontaran:~$ mkdir test
tcmal@sontaran:~$ cd test
tcmal@sontaran:~/test$ mkdir test2
tcmal@sontaran:~/test$ cd test2/
tcmal@sontaran:~/test/test2$ pwd
tcmal@sontaran:~/test/test2$ cd ..

pwd outputs the full path to whatever directory we're currently in. This path is absolute because it starts with a / - the filesystem root.

We can use ls to see what's in our directory.

tcmal@sontaran:~/test$ mkdir test3
tcmal@sontaran:~/test$ ls
test2  test3
tcmal@sontaran:~/test$ ls -al
total 16
drwxr-xr-x 4 tcmal 1004 4096 Oct 26 22:49 .
drwxr-xr-x 7 tcmal 1004 4096 Oct 26 22:49 ..
drwxr-xr-x 2 tcmal 1004 4096 Oct 26 22:49 test2
drwxr-xr-x 2 tcmal 1004 4096 Oct 26 22:49 test3

As well as positional arguments (like test in cd test), most Linux commands will also accept flags - starting with either a single - or two. In ls -al we've passed 2 short flags - a and l. These correspond to 'all' and 'long', and give us the long printout with the 'hidden' directories . and ... Because they're short flags, we only type one -, although we could type ls -a -l

Now that we know how to make directories, lets clean up after ourselves. Replace <username> with whatever your username is.

tcmal@sontaran:~/test$ cd /home/<username>
tcmal@sontaran:~$ rm --recursive test 

Note that we gave cd an absolute path - this is totally fine. We also could have said cd ~ - bash would have replaced the ~ with the same thing before it called cd.

When we call rm, this time we pass it the long flag recursive. Unlike short flags, you need to pass each long flag with a seperate '–'. The recursive flag is needed when you're deleting directories - like most long flags there is a corresponding short flag, so we could have said rm -r test

Learning how to use programs

The best way to learn how a program works and how to use it is by reading the man page (manual page) built into the system about that program. To find out what a program does and how to use it, simply type man [program name]. Try it now -man cat.

This takes us away from our shell and into a pager. You can scroll up and down using up and down arrows, page up and page down, and space and return. Don't worry if you don't understand everything written there, you're not expected to memorize all of a program's options - that's why they're so easily accessible in man pages! Press q to quit the man page reader.

Finding a program for your purpose

If you know what sort of program you want to run, but aren't sure of the name (or if such a program exists), you can use apropos to search for a program by function. For instance, say we want to find an IRC client but we don't know the names of any. We type: apropos irc But we get quite a lot of matches, most of which are no use to us. This is because the search has turned up a load of results where “irc” was part of another word, such as “circular”. If we have a quick look at man apropos we find out that the -e flag searches for exact matches: apropos -e irc

Yay, we've narrowed our matches down to what we wanted! Alternatively we could have tried apropos “irc client” which would have yielded the same results. However, typing apropos irc client without the quotes would have returned twice the unwanted results, as it would have searched for both “irc” and “client” and given you results for either. The quotes tell Bash to treat what you put in them as one continuous string.

Now go to the next tutorial to learn about how to create a web page that is automatically hosted by Tardis, on your own subdomain.

howto/terminal.txt · Last modified: 2023/09/15 10:39 by andrewferguson