LInux Exercises
LInux Exercises
CDT 2018
Susan Hutchinson
Department of Statistics,
University of Oxford
October 2018
Contents
1 Introduction 1
6 Conclusion 18
6.1 Linux answers 19
List of Figures
1 Adding a terminal window to the Favourites bar. 1
2 Starting applications 2
3 The nautilus file browser. 3
4 The nautilus file browser – modified. 3
5 Starting rstudio without &. 17
6 Starting rstudio with &. 17
7 Opening a terminal from Files. 18
1. INTRODUCTION
1 Introduction
First, a very brief look at the Fedora 28 Linux desktop. Next, a longer introduction to the
Unix/Linux command line.
This file available at http://www.stats.ox.ac.uk/pub/susan/cdt/Exercises.pdf. I recommend
that you open a copy on the desktop as many of the links are clickable.
We will now explore the Linux desktop and explain how to do some simple tasks.
2.1 Starting applications
Press the Windows key (or in Linux, the Super key) to start applications. You should see
a screen like Figure 2.
The bar on the left side is the Favourites bar which contains
1
2. THE LINUX DESKTOP
2
2. THE LINUX DESKTOP
directories. There may be some places where you can’t go or files you can’t view. They
will have a X on them.
On the bar at the top of the Files window towards the right is a small box with a 2x2 grid.
Click on this. This box allows you to change the file browser view. One click will make it
display more details about each directory. This is called the list view.
See if you can change the list view to include the owner and permissions, and changes
’Modified’ to ’Modified Time’. This is done by right clicking by Name and selecting options.
It should look like Figure 4.
3
3. THE LINUX COMMAND LINE
In the following sections, Linux commands are introduced and explained, then examples
and exercises are provided to illustrate some commonly used features.
3.1 Where am I?
This exercise explores Linux file system navigation.
4
3. THE LINUX COMMAND LINE
Note that all commands are typed in lower case. There are very few Linux commands
which have any uppercase (CAPITAL) letters.
You will be using these commands:
Command Purpose
ls [options] directory List files. If used on its own, it lists everything in the
current working directory (where you are currently lo-
cated).
file filename Tells you what sort of file the file named filename (for
example) is.
Right away we will see how quiet Linux commands are by default. Try typing in
cd
at the prompt and you will get no output at all. This does not mean that anything has
gone wrong. For many commands, no output means successful completion.
A digression on prompts. You can customise your prompt to look however you like. We
won’t do that now, but you may notice that it changes as you move around the file system.
Not all commands are silent. Try
pwd
You should get a response like: /homes/user . In all examples please replace user with
your username. Now try
ls
You should now see a listing of all the files in the directory /homes/user . Let’s find out
about some of the contents of /homes/user using the file command.
file TestDir
The shell tells you that this isn’t a regular file, it’s a directory. In other words it’s a special
file which acts as a holder for yet more files (like a folder in Windows).
file longfile.txt
longfile.txt: ISO-8859 text
so this file is a simple text file.
5
3. THE LINUX COMMAND LINE
Let’s explore the idea of relative and absolute path names using the cd command. Change
into the directory TestDir.
cd ~/TestDir
pwd
cd ..
pwd
cd ..
pwd
and so on until you can’t go any further (you won’t see an error, you just stop going
anywhere). .. is a special location which means up one level. All directories contain a
.. so you can go up a level. The exception is called / or sometimes “the root” or just
“slash”. You can’t go any higher than / so .. doesn’t take you anywhere. Note that there
is another special directory called . (a single dot) which means “current location”.
During the above task you went up the directories one level at a time. Now let’s reverse
the process and go back to the Desktop directory one level at a time. You should be in “/”.
You don’t need the pwds but it may help you see what is going on.
cd homes
pwd
cd user
pwd
cd TestDir
pwd
Remember always to replace user by your own username.
We’re now going to make use of two things, the ls command and the knowledge that the
file called /usr/share/icons is a directory, to further illustrate the concepts of absolute
and relative pathnames.
cd
cd ../../usr/share
6
3. THE LINUX COMMAND LINE
ls icons
and you will get a listing of the contents of the directory.
ls /usr/share/icons
and you should get the same list of files.
The absolute (i.e. complete) location of the icons directory is /usr/share/icons. We have
just asked to see what is kept inside it in two different ways. The first is a relative pathname
while the second is the full or absolute path.
Imagine the icons directory is a particular house, say 42, High Street, Abingdon and I
ask you to deliver a letter there. I could tell you to deliver the letter to “42 High Street,
Abingdon”: the full/absolute address. No matter where you are in the UK, that’s enough
information. However, if you were already in Abingdon I could tell you to deliver the letter
to the relative address of “ 42, High Street” or even better, if you were standing on the high
street just “number 42” would be enough.
The ls icons command worked because you were already in the /usr/share/ directory.
It wouldn’t work from another location. The command ls /usr/share/icons command
will work from anywhere (although it’s more long winded). Let’s prove it by changing our
current location using the cd command.
cd
cd TestDir
pwd
you should get /homes/user /TestDir i.e. you have moved into the TestDir directory.
ls /homes/user/TestDir
should give you a list of files in that directory. You could use ls on its own without the name
of the directory because you have already moved there with cd. Let’s see what happens
when we make a mistake:
ls TestDir
should give you an error saying there is No such file or directory which is correct.
The command fails because TestDir on its own is a relative path and you’ve started from
the wrong place.
Try to answer/do the following:
1. Were you just using absolute or relative paths in the last example?
2. Now try to get back to the root (or /) directory with one command only using an
absolute path.
3. Now get back to the /homes/user/TestDir directory using one command only.
4. What are the contents of the / directory? From your home directory use only one
command to find out.
5. Make sure you can see your data directory: /data/host/user.
See section 6.1 on page 19 for answers.
7
3. THE LINUX COMMAND LINE
Finally, a short cut! In order to change to a directory in your home directory, use the ~
character which represents your home directory to move to TestDir from any location.
cd ~/TestDir
Command Purpose
touch file1 file2 Create one or more empty file(s) called file1 , file2 ...
ls List files.
8
3. THE LINUX COMMAND LINE
the contents of a directory — this is known as a recursive copy. Finally remove the new
directory with
rm -rf directory2
Note that this is a dangerous command and should be used with care!
Use ls to check that this has worked.
You should now be familiar with these simple file manipulation commands. Remember
that in Linux the rm command really does delete files. There is no Recycle Bin to retrieve
files that were deleted by mistake.
3.3 Viewing files
In these exercises we explore commands to view the contents of files.
Command Purpose
less file Display the contents of file a screenful at a time, but with more
options. For example, after starting less enter G to go straight to
the end of a file and then move backwards.
Use the following commands to look at the contents of the file google.txt.
cd
cd Files
cat google.txt
This is not very useful if the file is more than a screenful.
more google.txt
Note that <space> takes you to the next page and q will quit before the end of the file.
Now try
less google.txt
See if you can get to the end of the file with a single command. Use google to help if you
like. Then use q to exit.
Speed things up
By now you may be fed up with all this typing! When using the command line in a terminal
window, there are ways to make life easier for you:
Filename and command completion
• <tab> key completes commands and filenames
9
3. THE LINUX COMMAND LINE
Command Purpose
man command Read the manual page for a command. So man ls would give details
of the ls command and man more of the more command.
apropos word Search the manual pages for names and descriptions which contain
word . So apropos copy would list all the commands that have the
word copy in the description.
what command you need, you can use the man command to find out the details of that
command. Try it with a few of the commands you have used already. Not all commands
have as many options as ls!
man ls
to find out details of the ls command.
i. What option is used to display modification time?
ii. What option is used to display the size of a file?
iii. How can you reverse the order of the sort so that the largest/most recently changed
file is at the bottom of the list?
iv. Check that they do what you expect.
Sometimes you might not be sure exactly what the command is. In that case you can use
the apropos command which finds all command descriptions which match a given word. So
to find out what commands there are to manipulate files are available use
apropos file
Note that the output from this command is very long! In a future session I can explain
how make this more useful.
Sometimes you need to know where Linux stores command. Use which to display the
location of the file. Try it with less, more, cp, apropos:
which less
which cp
10
4. LOGGING ON TO A REMOTE COMPUTER
which R
which pdflatex
Did you notice that R and pdflatex are stored in different places? The /usr/local directory
is used to share frequently used application so that we can provide a more up-to-date version
than that which comes with a standard installation.
Finally you may have seen a command and want to know briefly what it does. Use the
whatis command to find out. Try this on some commands you have learnt.
Command Purpose
ssh user @host .stats.ox.ac.uk Log on to a different system from outside the
department, named host.
11
4. LOGGING ON TO A REMOTE COMPUTER
Data in the first directory is a backed up daily, data in the second, never. There is a
system-wide limit of 400GB changed data per day for backups so please, if you are moving
a lot of data around, check with other members of the group to make sure they are not
doing the same thing.
Try this now. Log onto greywagtail and make sure you can find your data directories.
Note that every time you log onto a server (or any other computer in Statistics) you are
prompted for your password. We are going to speed things up now by removing the need
for passwords between Statistics computers.
4.1 Remove the password prompt between Statistics systems
Set up ssh keys so that you are not prompted for a password each time you move between
Statistics systems. In a terminal window on your desktop do the following:
i. Enter the command
ssh-keygen -t rsa
Generate a public key/private key pair. The following output should appear
Generating public/private rsa key pair.
Enter file in which to save the key (/homes/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /homes/user/.ssh/id_rsa.
Your public key has been saved in /homes/user/.ssh/id_rsa.pub.
The key fingerprint is:
20:30:e6:b0:5f:a0:aa:4f:d0:09:c7:b0:f4:25:45:4e
[email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|o.=.oE |
|.X.+= |
|= +.o.. |
|.= o . . |
|o + S |
|.. |
|. . |
| o |
| . |
+-----------------+
ii. cd
iii. cd .ssh
Note the dot before the directory name. Now check whether the file authorized_keys
exists. If it does use this command:
cat id_rsa.pub >>authorized_keys
if the file doesn’t exist use
cat id_rsa.pub >authorized_keys
12
5. MORE ADVANCED COMMAND LINE FEATURES
Command Description
Using
cd
cd Advanced
cat longfile.txt
you see all of longfile.txt; using
less longfile.txt
gives you a screenful at a time. Use the spacebar to move on a screenful, G to go to the
end of the file and q to exit. Now use
head longfile.txt
tail longfile.txt
to look at the first 10 lines with head and last 10 lines with tail.
13
5. MORE ADVANCED COMMAND LINE FEATURES
Now use the wc command to find out the number of lines, words and characters there are
in longfile.txt.
5.2 Using wildcards to match file names
File globbing or wildcard expansion allows you to use special characters to match more
than one file or directory name.
14
5. MORE ADVANCED COMMAND LINE FEATURES
Command Description
Command Purpose
> Sends the output from a command to the named file. If the file
already exists the previous contents will be lost. If the file doesn’t
exist it will be created.
» Appends the output from a command to the named file. If the file
doesn’t exist it will be created.
< Reads input from the named file. NB This option is rarely used.
| Uses the output from one command as the input to the next.
15
5. MORE ADVANCED COMMAND LINE FEATURES
Connecting commands together with pipes is one of the most powerful features of Linux.
Linux does not have a command to count the number of files in a directory but it does
have one command to list the files (ls) and a second command (wc) to count the number
of lines in a list.
You could therefore use a pipe (|) to glue ls and wc together:
ls | wc -l
Note that we are using the -l (that is a hyphen followed by the lower case letter l) argument
or option for wc. To get information about any command use
man command eg man wc
16
5. MORE ADVANCED COMMAND LINE FEATURES
Now we’re going build a longer command which will find the 5 largest files in a directory.
When building pipes of commands it often helps if you make sure each link in the pipe
works before adding the next. First display the size of all the files in the /usr/bin directory.
du -sk /usr/bin/*
Now sort this output by size, so that the largest are first.
du -sk /usr/bin/* | sort -nr
Now display the final 5 lines which will be the 5 largest files.
du -sk /usr/bin/* | sort -nr | head -5
Now how you would you find the 5 largest files in the /usr/bin directory beginning with
the letter ‘s’ ? See section 6.1 on page 19 for a solution.
5.5 Running commands in the background
When you start a command such as rstudio from a terminal window it is good practice to
add an & (ampersand) character after the command to keep access to the command line.
Compare Figure 5 and Figure 6.
Figure 5: Starting rstudio without &. Figure 6: Starting rstudio with &.
17
6. CONCLUSION
6 Conclusion
In future sessions we will look more at remote access to the grey* servers. If there are other
topics that you would like covered, please let me know.e
18
6. CONCLUSION
19