Linux Intro
Linux Intro
A comprehensive
introduction
Linux: A comprehensive introduction
11
Version 4.2 (TT 2019) 2 IT Learning Centre
Linux: a comprehensive introduction
Contents
1 Introduction...................................................................... 6
1.1. Aims for Today............................................................................6
1.2. Course Outline............................................................................6
1.2.1. Session One...........................................................................6
1.2.2. Session Two...........................................................................6
1.2.3. Session Three.......................................................................7
1.2.4. Session Four..........................................................................7
2 Getting started..................................................................8
3 Command line exercises..................................................21
4 Editors, regular expressions, and shell scripts................35
5 Using remote computers.................................................48
6 Answers........................................................................... 57
Exercises
Exercise 1 A first look................................................................................................. 9
Exercise 2 Changing the keyboard layout.................................................................10
Exercise 3 The home area......................................................................................... 11
Exercise 4 Getting to know the desktop....................................................................11
Exercise 5 Finding your way around.........................................................................14
Exercise 6 Finding applications not on the Dock......................................................16
Exercise 7 Word Processing......................................................................................16
Exercise 8 Slide shows.............................................................................................. 17
Exercise 9 StackExchange........................................................................................17
Exercise 10 Starting a terminal window...................................................................17
Exercise 11 Where am I? What’s all this?.................................................................18
Exercise 12 File and directory manipulation............................................................20
Exercise 13 Viewing files.......................................................................................... 22
Exercise 14 Absolute and relative pathnames..........................................................23
Exercise 15 Help commands....................................................................................24
Exercise 16 File and directory names.......................................................................26
Exercise 17 Looking at files......................................................................................27
Exercise 18 Using wildcards to match filenames......................................................27
Exercise 19 Searching and sorting...........................................................................28
Exercise 20 Pipes and redirection.............................................................................29
Exercise 21 Finding the largest file..........................................................................31
Exercise 22 Merge information from different files..................................................31
Exercise 23 Unpacking a longer example.................................................................32
Exercise 24 Using gedit............................................................................................ 36
Exercise 25 Simple regular expressions...................................................................37
Exercise 26 Regular expressions with spaces...........................................................39
Exercise 27 Finding noodles.....................................................................................40
Exercise 28 Changing what you've found.................................................................40
Exercise 29 An example shell script..........................................................................42
Exercise 30 Developing your first shell script...........................................................42
Exercise 31 File manipulation scripts.......................................................................47
Exercise 32 Setting up access to IT Services Linux system......................................49
Exercise 33 Using ssh............................................................................................... 50
Exercise 34 Copying files between systems..............................................................51
Exercise 35 Copying directories between systems...................................................53
Exercise 36 Managing sessions on remote systems..................................................53
1 Introduction
Today's course is divided into four parts each of which consists of a
presentation followed by exercises.
Getting started: the desktop, office applications, the terminal.
Using the command line.
Editors, regular expressions, shell scripts.
Working on remote computers.
2 Getting started
Here are some things to try to help you get to know Ubuntu Linux.
Let's look at the screen more carefully. The bar along the top has three components:
Various settings:
• Language and keyboard settings
• Volume control
• Battery
• Systems settings and logout
On the left side of the Desktop, below the Activities launcher is the Dock.
Under the Dock you should see a vertical list of popular applications. If you hold your
mouse over any of these icons then their name pops up.
From the top these are:
Install Ubuntu Install Ubuntu on your PC. Don't use this now!
Link to Amazon
The Install icon can be used to install Ubuntu on your PC, but please don’t do this!
At the bottom of the Dock is
Show Applications
• Help
From the top panel find
• System Settings [Hint: Look for the crossed wrench/screwdriver icon
under the arrow on the top right]
ii. Change your wallpaper. Ubuntu Live has a limited range of wallpapers; a full
install will have more.
iii. Linux supports the use of multiple workspaces or desktops. This allows you to
organise work more efficiently and reduce clutter. Press the Super (or
Windows) key on the bottom left of the keyboard:
You can then use the mouse to move between workspaces, and move
applications between these workspaces.
iv. More difficult: Change some keyboard shortcuts. Keyboard shortcuts use the
keyboard rather than the mouse to perform actions. Go to Keyboard in the
Devices page of System Settings) and use this to set up a quick way to switch
between workspaces. Scroll through this list of shortcuts, looking for Switch to
workspace 1 and replace Super+Home by, for example, CTRL+1 [Hold the
CTRL key and 1 down at the same time]. Switch to workspace 2 to CTRL+2
and so on. Do this for 4 workspaces. You should now be able to switch between
workspaces using these keystrokes. Some keyboard shortcuts are pre-
configured.
You can use Files to browse files and directories (the Linux equivalent of folders).
You should be in an area called Home. When you start the file browser, it opens in
your home directory. This is the place where your files will be stored by default – that
is if you don't specify another location.
Click on the small grid on the top bar
You can change the properties you see for each file and directory by right clicking
Name above the list of directories. A list of properties appears:
Change this list so that you can see the Type, Owner, Permissions and Modified –
Time.
Finally, see if you can find a directory that you can't see inside. We will talk about how
privileged accounts are used in the final session.
To find an application you can either browse for it by category or search in the search
box. In later sessions we will be needing a terminal window. Enter terminal in the
search box and see what happens. You may not need to type in the whole word to get
what you want.
Once you have found what you want, a single click on the icon will open the
application. As well as opening the application, an icon should also appear on the
Dock. If you right click on this icon, you can keep the application there by selecting
Add to Favorites, even after you have closed it. Test this with the terminal window.
The following two exercises look at the Libreoffice equivalents of Microsoft Word and
Microsoft Powerpoint. As time is short, choose the one you are most likely to find
useful.
Although most MS Office files work with LibreOffice, a few don't convert very well so
be warned that it's not 100%compatible. (Maybe 98% is a fair number).
Now close Writer and go to the StackExchange exercise.
Exercise 9 StackExchange
We will now look at another source of information and help that is available.
StackExchange provides a gateway to communities of expertise. A particular benefit of
the sites is the lack of additional chat – in general there is a strong focus on direct
answers to questions. Browse to http://stackexchange.com/about to find out more.
Browse to http://www.stackexchange.com and click on 'Explore our sites!' to see the
huge range of topics covered. Let's investigate the 'Ask Ubuntu' site. On my browser
the link to this site appears in a large orange box to the right of the large
'Mathematics' box which is near the top on the left hand side. If you can't find the link
then use the browser search feature (usually CTRL+F) and enter Ubuntu.
When you click on the 'Ask Ubuntu' box you should see a 'Visit Site' button. Click on
this.
Now click on 'Take the 2-minute tour' to see a brief guide.
If, at the end, you have time have a look at other useful sites such as 'Unix &
Linux'.
Click on 'Activities' and enter Terminal in the search box. When you have found the
terminal open it. If you want to keep it on the Dock, right click on the icon on the left
hand side of the screen and select 'Add to Favourites'.
You should see a Window open that looks like this:
Note that it's perfectly possible to have a command line with no graphical desktop at
all. This is often the case with server systems which are not used interactively and
need all the processor power and memory they can get for computation.
We will be finding out more using remote server systems in the final section.
Command Purpose
file filename Tells you what sort of file the file called
filename (for example) is.
Note that all commands are typed in lower case. There are very few Linux commands
which have any uppercase (CAPITAL) letters. We will look at case-sensitivity and file
names in the next session.
Right away we can see how quiet Linux commands are by default. Try typing in
cd
at the ubuntu@ubuntu:~$ 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 will notice that it changes as you move around the file
system.
Not all commands are silent. Try
pwd
You should get a response like: /home/ubuntu. Now try
ls
You should now see a listing of all the files in the directory /home/ubuntu. Let's try
finding out what sort of files each is. Take the file “Desktop”.
file Desktop
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). If you
now try
file Desktop/*
you should see a description of the two files in that directory.
TIMESAVING INFORMATION
If you haven't already tried this then you should now. A lot of typing can be avoided by
several useful shortcuts. The <tab> key can be used to complete commands file
names and the arrow keys to recall previous commands and perhaps change them.
Command Purpose
cd
mkdir directory1
cd directory1
touch file1 file2 file3 file4
Remember that words in italic should be replaced by names that you have chosen.
Experiment to see what happens if you are not in your home directory. What happens
if you try to create a directory in /usr/bin? Is there anywhere outside your home
directory where you are allowed to create directories? [Hint: look at the top level
directory / - you should be able to create a files and directories in one of those. The
name of the directory might also be a clue. Check in the Answers section below.]
Use the cp command to copy one file to another and then use ls to check that you
have done what you want. Then delete a file using
rm file1
Now we are going to copy one directory to another. The commands you need are
cd
cp -r directory1 directory2
Use ls to make sure you have done what you want. The new directory should contain
exactly the same files as the old one. Note use of the -r option. This makes cp copy
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.
Command Purpose
Use the following commands to look at the contents of the file google.txt.
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 go straight to the end of the file. Then use q to exit.
1
This command is not available on MacOS. Use curl instead. For example
curl -O http://www.stats.ox.ac.uk/pub/susan/linux/LinuxFiles.tgz
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 “the root” or
just “slash”. You can't go higher than / so “..” doesn't take you anywhere. Note that
there is another special directory called “.” which means “current location”. All
directories contain a “.” directory and we'll see why it is needed later.
During the above task you went up the directories one level at a time. Now reverse the
process and go back to the Desktop directory one level at a time. You should be in “/”.
Note that you don't have to do the pwds but it may help you visualize what is going on.
You can also use ls to have a look around each level if you have time.
cd home
pwd
cd ubuntu
pwd
cd Desktop
pwd
Try to answer/do the following:
Were you just using absolute or relative paths?
1. Now try to get back to the root (or “/”) directory with one command only using an
absolute path.
2. Now get back to the /home/ubuntu/Desktop directory using one command only.
3. What are the contents of the “/” directory? From your home directory use one
command only to find out.
Command Purpose
Command Purpose
If you know what command you need, you can use the man command to find out the
details of that comand. 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.
1. What option is used to display modification time?
2. What option is used to display the size of a file?
3. How can you reverse the order of the sort so that the largest/most recently
changed file is at the bottom of the list?
4. 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. We will look at a neat way round
this in the next session. Here is a real life example. I needed to know which command
in this Ubuntu distribution was the equivalent of Microsoft Draw so I ran
apropos draw
See if you can see the name of the command I used. The answer is at the end of the
exercises.
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 apropos
which cp
which more
which less
Did you notice that more and less are stored in different places? The directory /bin
conventionally is used to store a few necessary commands that can be used if all else
fails. 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.
Command Purpose
Command Description
less file Display the contents of file a screenful at a time but with
more options. For example, after starting less you can
enter G and go straight to the end of a file and move
backwards.
man command Read the manual pages to read all about a command. So
man head
would describe how to use the man command.
Using
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
and
tail longfile.txt
to look at the first and last 10 lines of the file.
Now use the wc command to find out the number of lines, words and characters there
are in
longfile.txt.
Change back to your home directory (if you are not already there) and then change to
the directory called WildCards.
cd
cd WildCards
Now experiment with wild card characters. What do the following match?
ls foo?
ls foo2*
ls foo[1-2]
What command would you need to match just foo20 and foo2bar? [Hint: you might
need to use more than one wild card character.] The answers are at the end. Now use
wc -l *
to find the length of each file. Note that the -l is a hyphen followed by the lowercase
letter l. You should see output like this:
1 foo
2 foo1
2 foo10
1 foo2
1 foo20
3 foo2bar
10 total
Now see if you can create a match so that wc -l just shows the files with a 1 (the
number one) in their name. Again a possible answer is at the end.
Command Description
and we will be using the following characters. If you haven't already remapped your
keyboard you should do that now. See Exercise 2.
Character Purpose
Almost all Unix/Linux commands use standard input for receiving instructions and
standard output for displaying the results. So we could run
grep green fruit veg > output
which would store the output of the grep command in a file called output. Check this
with
cat output
If you run the command again, this time using
grep green fruit veg >> output
you should see that there are now two copies of the output. It is also possible to use <
redirect the input from the keyboard to a file. For example
cat <output
also works.
In the previous exercise we used grep to look for occurrences of the word green in
two files. Obviously in this short example we can count how many times green
appears, but when there are many matches it would be useful to use wc to find out. We
can redirect the output from the command into a file.
Note that in all cases the -l option to the wc command is a minus sign followed by the
lower case letter l.
grep green fruit veg >output
and then run wc on the file
wc -l output
but it would be much more efficient to join the two commands together with a pipe.
Use
This is a rather contrived example but demonstrates some very useful Unix/Linux
commands. Make sure you are still in the Searching directory.
cd
cd Searching
Now we are going to use the awk command to print out only the first column of the file
called creatures. Have a look at the file before you run this command.
awk '{print $1}' creatures
What do you think would appear if you replaced $1 by $2? Make sure you copy the
command exactly. In particular you need the single quote character and curly
brackets. Now change this to
awk '{print "A", $1, "eats"}' creatures
As well as a list of animals you should also see that the words “A” and “eats” appears
either side of each animal. Now we are going to use the paste command to include the
output from the fruit file.
awk '{print "A", $1, "eats"}' creatures | paste -d" " - fruit
Again, make sure that you have entered the commands exactly as they appear here. To
explain what the paste command is doing: the -d” “ option makes a space rather
than a tab appear before the name of each fruit, the – before fruit means that the
output from awk... is included.
Now see if you can make the fish eat the vegetables!
Command Description
7 yahoo
8 engine
9 and
9 on
10 pages
10 web
11 in
12 of
13 its
14 a
15 search
16 google
20 to
29 the
This is a list of the 20 most common words used in the file longfile.txt. Now, from
the command line, we’re going to run the command repeatedly, each time removing
the final element, to unpack how it works.
Using the mouse, copy the contents of my_wordcount and paste into the command
line. You should have a very long command like this:
sed -e 's/\.//g' -e 's/,//g' -e 's/\!//g' -e "s/\'//g" -e "s/\"//g" -e
's/-//g' longfile.txt | tr ' ' '\012' | tr [:upper:] [:lower:] | sed -e
'/^$/d' | sort | uniq -c | sort -n | tail -20
This looks very complicated!
Now the list is sorted alphabetically rather than numerically. Again, remove the final
pipe,
| uniq -c
and see what happens.
sed -e 's/\.//g' -e 's/,//g' -e 's/\!//g' -e "s/\'//g" -e "s/\"//g" -e
's/-//g' longfile.txt | tr ' ' '\012' | tr [:upper:] [:lower:] | sed -e
'/^$/d' | sort
Now you should see an alphabetical list of all the words, but with multiple occurrences
of many words.
Remember you can always add
| less
on to each version of the command so that you can look at the output a screenful at a
time.
The uniq command (which is very useful) removes duplicates, and with the -c option,
adds a count of the number of times it has found a match. Now remove the final pipe,
| sort
again so that you have
sed -e 's/\.//g' -e 's/,//g' -e 's/\!//g' -e "s/\'//g" -e "s/\"//g" -e
's/-//g' longfile.txt | tr ' ' '\012' | tr [:upper:] [:lower:] | sed -e
'/^$/d'
This time you have all the words in the file with one word on each line but in the order
they appeared in the original file, not sorted alphabetically. You can read the contents
of longfile.txt if you want to check this. Now again remove the last pipe again
| sed -e '/^$/d'
and see what the output looks like.
sed -e 's/\.//g' -e 's/,//g' -e 's/\!//g' -e "s/\'//g" -e "s/\"//g" -e
's/-//g' longfile.txt | tr ' ' '\012' | tr [:upper:] [:lower:]
The file no longer contains any blank lines. If you haven't already add | less on to
this version and the previous version to compare. Again, remove the final pipe,
| tr [:upper:] [:lower:]
and see what happens.
Did you notice that some uppercase letters appeared? The tr command allows you to
translate either classes of characters (such as all uppercase characters) or individual
characters to something else. In this case we are translating all uppercase characters
to their lowercase equivalent. Now remove the final pipe again.
sed -e 's/\.//g' -e 's/,//g' -e 's/\!//g' -e "s/\'//g" -e "s/\"//g" -e
's/-//g' longfile.txt | tr ' ' '\012
Now you should see the file with many words on each line. In this case we have used
tr to translate all space characters to the newline character (which is represented by
\012).. The first command – which is very complicated - removes some punctuation
from longfile.txt.
sed -e 's/\.//g' -e 's/,//g' -e 's/\!//g' -e "s/\'//g" -e "s/\"//g" -e
's/-//g' longfile.txt
This is a complicated command! We are using the sed editor to match and remove
several separate things. Each match begins with -e and involves replacing various
punctuation characters with nothing at all. In some cases the punctuation character is
preceded by “\” because it has special meaning in the sed editor.
Try them! Now see if you can enter some text into the file, save the text and quit gedit.
Now start the editor up again with the same file, change the text, save it and quit
again.
A small digression. When you started gedit you entered gedit newfile &. The &
character has a special function. It allows you to start a command and continue using
the terminal window to enter further commands. Technically it is called 'running a
program in the background'. See what happens if you don´t include the & - just type in
gedit newfile. You will need to exit gedit or enter CTRL+C [ie press the CTRL key and
C at the same time] to get back control of the command line.
A brief summary of some frequently used command line control sequences are
CTRL+C interrupt a running program
CTRL+D send an end of file, ending text input for most Linux/Unix programs
CTRL+Q unfreezes the screen if CTRL-S has been used to freeze it.
CTRL+U deletes the last line typed
CTRL+Z suspends a running program. Use bg to continue running the program in
the background or fg to continue running the program as it was.
Command Description
Have a look at the file HAYSTACK. It's going to be easier to look at it in gedit so enter
cd
and select Preferences. On the View tab select ‘Display line numbers’ which should be
the first item.
We'll spend more time looking at gedit in the next session, but for now we're going to
use it to show the contents of a file.
Have a look at the file. There are many lines of text with mostly the word 'hay' none or
more times. There are some needles hidden in there, as well as some nedles, neeedles
and noodles.
A simple search
grep -n -E needles HAYSTACK
will show all the occurrences of needles in the file.
grep -n -E ^needles HAYSTACK
will show the lines that contain needles right at the beginning of the line. The search
grep -n -E needles$ HAYSTACK
will show only the lines that have needles at the end of the line.
How would you search for a line which contains only one occurrence of needles and
nothing else?
See the Answers section to find out.
Command Description
Sometimes words at the beginning of a line are preceded by white space (either a tab
or a space). Regular expressions have many character shorthands which allow us to
make general searches. In this example we use '\s' to match a single white space
character.
grep -n -E '^\sneedles' HAYSTACK
to find all the lines that begin with a single white space and then needles.
What if there are multiple spaces? We can use the '+' character which will match one
or more instances:
grep -n -E '^\s+needles' HAYSTACK
How many lines end with one or more white spaces?
grep -n -E '\s+$' HAYSTACK | wc -l
Note that this time, we haven't restricted the search just to lines ending in needles.
In some places hay and needles have been run together. To find only the places where
needles is separate from hay use
grep -n -E '\bneedles\b' HAYSTACK
This is a particularly useful search because it means we don't have to worry about
matches for beginning and ends of lines. Incidentally we can reverse this by using the
grep -n -E '\Bneedles' HAYSTACK
and
grep -n -E 'needles\B' HAYSTACK
which will find instances where needles are joined to hay either at the beginning or
end of the word.
What happens if you use
grep -n -E '\Bneedles\B' HAYSTACK
See the Answers section.
Command Description
Looking at the contents of HAYSTACK, we can see that there are words similar to
needles - noodles, nedles and neeedles. How can we find them all?
We could use the alternation character: '|' to find needles or noodles.
grep -n -E 'needles|noodles' HAYSTACK
This is a little restricted as we may need to specifiy many alternatives. Try this:
grep -n -E 'n.{0,2}dles' HAYSTACK
This will match all strings that
• start with 'n'
• have 0, 1 or 2 characters before 'dles'
Command Description
Use the above to change all occurrences of nedles, needles, and neeedles to noodles.
Check that
grep -n -E 'ne{0,3}dles' HAYSTACK
finds all occurrences of nedles, needles and neeedles in HAYSTACK. Then enter
sed -r -e 's/ne{0,3}dles/noodles/g' HAYSTACK | cat -n
The '| cat -n' isn't necessary, but adds line numbers to the output to show which
lines have been changed.
Note that the regular expression used by sed is the same as the one used by grep.
This is a particularly useful feature of regular expressions.
Finally, send the output to a new file, NEW_HAYSTACK.
sed -r -e 's/ne{0,3}dles/noodles/g' HAYSTACK >NEW_HAYSTACK
All your nedles, needles and neeedles should now be noodles! You can compare the
two files with
diff HAYSTACK NEW_HAYSTACK
This completes the exercises on regular expressions. This is a very brief introduction:
they are a powerful and flexible tool.
#!/bin/bash
echo "Hello world"
cp hello hello1
#!/bin/bash
echo "Hello $1"
New concept: you can choose who to greet using a command line argument. This is
interpreted by the script with $1 .
• Now edit the file so that it looks like the version 2 of the script
• Run the script with a command line argument and check that it works
./hello Marge
You should see the output
Hello Marge
Again copy the “hello” file to a new file “hello2” to save a known working version.
Now repeat these steps for the four further examples:
#!/bin/bash
for name in $*
do
echo "Hello $name"
done
New concepts:
• $* matches all the arguments entered on the command line
• the for name in list construct is particularly useful for operating on command
line arguments. The variable $name is assigned to each argument in turn and then
evaluated when used with echo .
After editing the file to make the above changes, run the command
./hello Bart Lisa Maggie
This should produce the output
Hello Bart
Hello Lisa
Hello Maggie
#!/bin/bash
if [ $# -eq 0 ] ; then
echo "No one to say hello to"
exit 1
else
for name in $*
do
echo "Hello $name"
done
fi
New concepts:
• $# contains the number of arguments supplied. You can check this by adding the
line
echo $#
above the line beginning if [ $# eq 0 ...
• Control flow – the idea that you can choose what happens next based on a condition
being met – is introduced using if expression then else fi.
• exit 1 causes the script to stop
After editing the file to make the above changes, run the command
./hello
You should see the output:
No one to say hello to
Now run the command for name in list
./hello Homer Marge Bart
which should produce the output
Hello Homer
Hello Marge
Hello Bart
as in version 3.
#!/bin/bash
if [ $# -eq 0 ] ; then
echo "No one to say hello to"
echo "Please enter people to greet"
read people
echo $people
else
people=$*
fi
for name in $people
do
echo "Hello $name"
done
New concepts:
• Prompting for input from the script. Instead of stopping when no command line
arguments are supplied the script now prompts for input. The name or names
supplied are stored in the variable people. At this point the names are stored in one
of two places so the else clause is used to move the contents of the command line
argument to the variable people.
• The for name in loop now uses people rather than $* for this list of names.
Make the above changes and run the command
./hello
now produces the output
No one to say hello to
Please enter people to greet
enter the names:
Homer Marge Bart
and hit return.
you should see
Hello Homer
Hello Marge
Hello Bart
Running
./hello Homer Marge Bart
will also produce
Hello Homer
Hello Marge
Hello Bart
Now for you to try a couple of things.
• Create a test within the for loop to check for a particular name – say Homer – and
change the message for this name. Hint: the test might look something like this.
if [ $name == "Homer" ]
• Create a small file called Names containing a few names. Now run the following
command:
cat Names | xargs ./hello
Note how easy it is to change where the input comes from. You can download an even
longer list of names from here using
wget https://www.stats.ox.ac.uk/pub/susan/linux/Simpsons
#!/bin/bash
if [ $# -eq 0 ] ; then
echo "No one to say hello to"
echo "Please enter people to greet"
read people
else
people=$*
fi
for name in $people
do
echo "Hello `echo $name | rev`"
done
New concept:
The line beginning echo “Hello ... has been changed. Looking at this in more detail
shows that the there is `(backquote) character before echo $name and after rev. This
means that echo $name | rev is executed before being displayed on the screen. The
effect of this is to pipe the output from echo through the command rev, which simply
reverses the output.
The backquote is found at the top lefthand side of the keyboard next to 1.
#!/bin/bash
first=$1
last=$2
root=$3
Save this in a file called create, give the file execute permission and then use
./create 1 4 newfile
which would create four files called
newfile1 newfile2 newfile3 newfile4
A while loop is used to repeatedly create files until the number specified is reached.
The line let first=$first+1 adds one to first each time the loop is executed.
Change this script to check that the right number of command line arguments have
been given. Print out a message and stop if there aren't three arguments.
The answer is in the Answers section.
Now we will look at a script to copy all files with the same root to another name.
#!/bin/bash
let n=1
for file in $1*; do
cp $1$n $2$n
let n=$n+1
done
[More difficult.] Change the script so that the new file name has a dot after the
number and then the date of the form 03Mar13 so that the files look like
test1.27Feb13 ...
You will need to look at the date command – use date --help or man date and
Version 6 of the hello script. The answer is below.
Note that there is a very useful command rename which will rename one or more files
based on a regular expression. It may not be installed by default. If you want to
investigate this enter
sudo apt-get install rename
rename --help
apt-get install is the command line tool for installing extra command or packages.
There is a brief discussion of sudo in the next session.
Concluding Remarks:
These exercises can only scratch the surface of what can be done with the command
line and shell scripts. Don't expect everything you've seen today to sink in. Working
with command lines takes some getting used to and the best way to learn is by lots of
doing. For now, simply take away the ideas of what this environment is capable of.
Click on this link and you should now see the screen to activate the account.
If you are asked to choose a shell, make sure you choose /bin/bash. You should then
Command Description
Use
ssh [email protected]
at the login prompt enter your SSO and password. So if your SSO is coll1234 you
would type in
ssh [email protected]
You should see a screen like this:
The commands that you have used on the live Ubuntu, will also work on this system,
although it is running a different Linux distribution. It is running Debian Jessie.
Use the cd, wget and tar commands from Exercise 14 to download the files to this
server.
cd
wget https://www.stats.ox.ac.uk/pub/susan/linux/LinuxFiles.tgz
tar -xvzf LinuxFiles.tgz
Make sure that commands are behaving in the same way by using examples from
previous exercises.
Command Description
On linux.ox.ac.uk enter
xclock &
Command Description
@ Location indicator.
See if you can copy HAYSTACK from the live Ubuntu session to the same location but
with a different name.
See if you can use wild cards to copy all the files beginning foo2 from the Wildcards
directory to the same location and with the same name.
Use ls Copies on linux.ox.ac.uk to make sure the files have copied successfully.
Command Description
Press return and you should see the standard prompt. Now use wget to get the script
you will be using
cd
wget https://www.stats.ox.ac.uk/pub/susan/linux/my_date.sh
Permanently change the permissions, so that the script can be executed.
chmod +x ./my_date.sh
Have a look at the my_date.sh script to see what it does. Don’t worry if you can’t
understand every line.
cat my_date.sh
#!/bin/bash
# Ask for name.
# Repeatedly output the name and date, waiting 15 seconds.
# Terminate after 15 minutes.
echo "Please type in your name"
read name
start=`date +%s`
now=$start
while (( (( $start + 900 )) > $now )) ;
do
echo "Hello $name, the time now is `date`"
sleep 15
(( now += 15 ))
done
The script displays the date and time every 15 seconds for 15 minutes. Run the script
interactively to watch what happens.
./my_date.sh
Watch the output appearing for a minute or so. You should see something like this:
Now we are going to detach from the screen session so that the script will continue
running but we will logout of linux.ox.ac.uk. To detach from the screen session use
^CTRL-a d
That is, hold down CTRL and a, then press D. You should see a message
[detached from XXXX.pts-XX.raven]
where each X is a digit.
You can check what screens you have with
screen -list
and should see something like this:
There is a screen on:
6144.pts-30.raven (01/23/17 17:29:58) (Detached)
1 Socket in /var/run/screen/S-hutchins.
This is a slightly contrived example. In general output from long running jobs is best
saved in a file rather than displayed on the screen, as there limits to the amount of
data that a terminal displays. However, the technique for detaching the screen and
logging out is the same.
Concluding Remarks:
Accessing remote computers is a common requirement for Linux users. On Windows, a
small application called PuTTY provides a secure means for logging on to a Linux
system. On Macs, use the terminal window.
6 Answers
Session 1
Exercise 3 Try Dock | Files
Exercise 4 The active application is highlighted in a lighter coloured square. For
each open application an orange dot appears.
Exercise 4 Keyboard shortcuts
To create keyboard shortcuts to switch between desktops
1. Open System Settings | Keyboard
2. Click on Navigation
3. Scroll down until you find Switch to workspace 1
4. Click on Disabled
5. Hold down the Ctrl key and press F1 (on the top lefthand side of the
keyboard)
6. Now right click on Switch to workspace 2
7. Set this to Ctrl and F2
8. Do this for workspace 3 and workspace 4 ...
9. Click on X to save the changes.
If you now hold down the Ctrl key and press F2 you should switch from
Desktop 1 to Desktop 2
Session 2
Exercise 16 Finding a drawing application
This is straightforward:
apropros draw
lists several possible matches. The most useful is
lodraw (1) - OpenOffice.org office suite
If you enter lodraw & on the command line the application will start.
Exercise 17 Reading a file called *star
cat \*star
cat “*star”
are both possibilities. A \ (backslash) is an escape character which stops the star
being interpreted as a wildcard. Surrounding file names that contain special
characters with quotation marks often work. There may well be other ways to do this.
Excercise 19 Wildcards
ls foo? matches
foo1 foo2
ls foo2* matches
foo2 foo20 foo2bar
ls foo[1-2] matches
foo1 foo2
To match just the files foo20 and foo2bar use
ls foo2?*
To use wc -l just to display the files with a 1 in their name.
wc -l *1*
Session 3
Exercise 26 Simple regular expressions
grep -n -E ^needles$ HAYSTACK
will find all lines with just one occurrence of the word needles with no white space
either before or after. We'll look at how to match white space in the next set of
exercises.
Session 4
Exercise 35 Copying a single file between systems
scp HAYSTACK [email protected]:~/Copies/HAYSTACK2
scp foo2* [email protected]:~/Copies/.
Copyright
Susan Hutchinson makes this document and the accompanying LibreOffice Impress
presentation available under a Creative Commons licence: Attribution,
Non-Commercial, No Derivatives. Individual resources are subject to their own
licencing conditions as listed.
The Oxford University logo and crest is copyright of Oxford University and may only
be used by Oxford University members in accordance with the University’s branding
guidelines.
Acknowledgements
This course was originally developed with Jon Lockley.
Version information
Minor updates – replace Usenet with StackExchange May 2014 SRH
Updated for Ubuntu 14.04 (Trusty Tahr) Sep 2014 SRH
Minor corrections Jan 2015 SRH
Minor corrections and checking. Regular expressions. Nov 2015 SRH
Major changes to Section 4: replaced software installation with remote Apr 2016 SRH
access to linux.ox.ac.uk. SRH
Jan 2017
Added branded front page Apr 2017 SRH
Corrections for 2017-2018 Sep 2017 SRH
First section rewritten for Bionic Beaver, 18.04 Oct 2018 SRH
Typos fixed immediately after MT2018 presentation
Updated footer for HT 2019 Jan 2019 SRH
Minor corrections TT 2019 Apr 2019 SRH
MT 2018
Your comfort is important
Session 1
• The toilets are along the corridor outside Introducing the
the lecture rooms.
• The rest area has vending machines and
Linux desktop
a water cooler.
• The seats at the computers are
adjustable.
• You can adjust the monitors for height,
tilt and brightness.
IT Services Linux Courses Session 1
If you are comfortable using office applications, browsing the web and using
email then this course aims to show that you can use Linux without too steep a
learning curve. The first two sessions focus on the similarities between Linux
and other operating systems such as Windows and Mac OS.
In the remaining sessions we start to explore the differences between Linux and
Microsoft Windows. The course demonstrates many powerful features that
have no equivalent in Windows.
How Linux changed things What is Linux?
●
Linux is an operating system designed by Linus Torvalds in the early
Linux does not come from a single large corporation. 1990s.
●
It offers an alternative approach. An operating system protects the user from the hardware and vice versa.
– Freedom of choice. An operating system has several parts:
– Freedom to understand and change. – kernel
– Software written with quality rather than profit as a – system calls
goal. – programs
●
Competition and alternative approaches benefit users Other Operating Systems include
and consumers.
– Windows (7, Vista, XP, 2000, 95)
– Software developments: firefox for example.
– MacOS
– Increased awareness of open standards by Microsoft.
– Unix
– Easier to work using different systems.
The Operating System manages resources such as the processor, disks and
Let's take the example of browsers. Internet Explorer is now quite a good memory, and provides a consistent interface through system calls for
browser, but it needed Firefox – and now Google Chrome – to make this application programs. This means that application developers do not need to
happen. For example, would tabbed browsing have been added to IE know details of the hardware.
without Firefox? Furthermore, issues around security in browsers have
been hugely improved. In this case competition has improved the An amusing analogy to give you a flavour of different operating systems is “If
experience for everyone. Operating Systems Ran Airlines”. Enter this into a search engine to find out
more.
Linux is related to Unix and certainly appears very similar but works rather
differently underneath.
There are other free Unix operating systems if Linux doesn't appeal: see
www.freebsd.org for example.
Who makes Linux? How is Linux developed?
There is more information about GNU, its relationship to the Linux kernel and
the GPL (General Public Licence) at www.gnu.org .
GPL is the standard licence for most releases of free, open source software.
Several different notions of freedom are incorporated in the idea – freedom to
change, freedom to understand, freedom to distribute.
One very important point to remember is that all Unix jokes are truly awful.
What is open source? What is Linux used for?
In Windows .exe files contain machine-ready instructions. In Linux [and Much of the University infrastructure, particularly at IT services, is based on
Unix] these sorts of files are called binaries (although they are usually machines running Linux. IT services organise a tour of the machine room
executable). This is because there are also human readable executables every year and you'll find a penguin badge on most things.
or “scripts” which are lists of commands. Further Linux [2] looks at scripts.
The web also relies heavily on Linux – many web servers are Linux based
What are the advantages of building software from source? When you are machines.
provided with only the binary you can be sure that it wasn't built on your
machine. Building your own binary from source means that you can specify Linux desktop systems were very poor compared with commercial
that it should be optimised for your own hardware and customised with alternatives until a few years ago. This made it unlikely to appeal to non-
features that are or are not needed. technical users who were using MacOS or Windows. Linux also lacked any
good office software. Both of these areas – as we hope to demonstrate –
If something goes wrong you could debug the software – or at least get have now been addressed very successfully. However this has left a legacy:
someone else to do it for you. You can also change the way it works if it Linux still has the reputation of being hard to use.
only nearly does what you need.
The easiest way to get Linux is to download or to buy a “distro” or The two big distros are RedHat/Fedora and Suse:
distribution.
A distribution = a kernel + applications + installation software+
support + documentation.
Typical applications are
●
Office software
Debian Mint
●
Databases
●
Programming languages and tools
● Mepis Slackware
Web browsers
●
Email readers
●
Internet services (www, DNS, NIS, firewalls) Mageia arch
●
Games
Most distros come with a version number – for example Fedora 28 or There is no technical reason why a RedHat, Fedora or openSuse (now
OpenSuse 18.04 but they are not particularly significant. When acquiring owned by Novell) installation cannot be done at home. However, in recent
Linux the important thing is to find out what version of the kernel is supplied years both distros have changed their product lines to focus more on the
and what versions of software are included. It is simple – with Internet business user. RedHat are also involved with a free distro called Fedora
access – to upgrade to the latest version but it is good to start with relatively which is now stable. There is also another distro, CentOS, which is based on
recent versions. RedHat.
Current kernel versions are 3.16.x, 4.14.x, 4.18.x Of the other free distributions Debian is the most mature and stable but is not
recommended for the newbie (beginner in Linux-speak). Debian variants like
● Ubuntu are more friendly. Mandriva, Mint and particularly Ubuntu are all
Office software might be LibreOffice or Calligra
● aimed at the home user and are a good place to start if you want to try linux
Databases might include mySQL
● at home.
Programming languages should include C, C++, Fortran, Perl and Python
●
Web browsers might include Opera, Mozilla Firefox, Midori and
Chromium Free Linux distros can be downloaded from www.linuxiso.org .
●
Email readers might include Mozilla Thunderbird, Kontact, Gnome Gmail
See http://distrowatch.com/ for a discussion on recent Linux developments.
Generally, Firefox and Thunderbird, the browser and mail reader from the
Mozilla stable are included. Both of these are also available from
www.mozilla.org and have versions for Windows as well as Linux.
Ubuntu Linux and Windows
We are using Live Ubuntu for this course because it does not change or use
the hard drive and so IT Services does not have to rebuild machine Because Windows is still the dominant desktop system Linux works hard to
especially for this course. This also means that you can try this version of support Microsoft standards so that Linux and Microsoft users can work
Ubuntu at home safe in the knowledge that is very, very difficult to damage together.
your machine or the data you have on your disk.
VMware creates a virtual machine which can run binaries for the operating
The disadvantage of live systems is that it is more fiddly to save data system it emulates. You can run a virtual Windows machine under Linux or
permanently because the hard drive is not easily accessible. However some the other way round. VMware is not free or open source.
people find live Ubuntu and a USB memory stick a very useful combination.
The slides and exercises were prepared like this. Often, you will need to Wine is an open source copy of the Windows system libraries which allow
install software as there is only a limited range on the USB. many Windows .exe files to run under Linux. Many software packages run
faster under Linux+Wine than under Windows!
If you decide that you like Linux it is better to install a more permanent
version on your hard drive. WineX is a more powerful but not free version
To download Ubuntu go to http://www.ubuntu.com. We are using Ubuntu Oracle (formerly Sun) provide an open source product, VirtualBox which is
18.04 LTS (Bionic Beaver). LTS versions will continue to get updates for 4 becoming increasingly popular.
years after release, other releases need upgrading after about 1 year.
Desktop Environments
●
Desktop systems in Linux come with a variety of
desktop environments.
Office applications ●
Provide an easy to use graphical use interface
or GUI based on the X window system.
●
Choice: KDE or Gnome are the most widely
available.
●
Generally they provide very similar functionality.
●
Competition has produced two very usable
solutions.
If you are comfortable using office applications, browsing the web and
using email then this course aims to show that you can use Linux
without too steep a learning curve. The first session starts by focusing
on the similarities between Linux and other operating systems such
as Windows and Mac OS.
Unix systems have had a graphical user interface for some time but
these have been largely proprietary. Clearly the GNU/Linux project
needed an open source desktop environment that would provide an
intuitive user interface.
●
LibreOffice runs on Windows just as well as on Linux Microsoft Office products and their LibreOffice
and Unix. equivalents:
●
LibreOffice supports all Microsoft data formats. Microsoft Word = Writer
●
LibreOffice offers almost all the functionality that
Microsoft office does (a few fancy bits might be
Microsoft Excel = Calc
missing). Microsoft PowerPoint = Impress
●
LibreOffice was far more standardised than Microsoft Microsoft Paint = Draw
Office but the latest version of Microsoft Office does
allow you to save files in an open standard (XML)
format.
Most Unix users used (and Mathematicians still use) TeX and LaTeX (the X is
pronounced to rhyme with “ch” as in loch) to produce documents. These are very
All of these applications are installed in Live Ubuntu. The word processor,
powerful typesetting tools and give far more control than word processors. They are
spreadsheet and presentation software can be found are all available from
excellent for writing theses, books and papers, particularly those with formulae and
the Dash and the draw application can be found by clicking on the Dash
pictures but not suitable for short documents and letters. They also have a slide-
icon and searching for Draw.
making environment which is fairly simple to use simply but can be rather difficult to
use for complex presentations.
There were other typesetting programs such as nroff/groff/troff which were used for
creating manual pages. As you may now predict there are a range of packages
available which provide office productivity tools. These usually include a document
creator (or word processor), a spreadsheet, a tool for creating presentations and a
drawing package.
Microsoft files do not always read perfectly in LibreOffice. Microsoft – as is their right –
do not reveal how data in MS files is formatted and these formats are changed from
time to time. This means that LibreOffice software developers have to respond to
these changes and “reverse engineer” files in order to code software to read files.
Clearly not sharing document formats is a good way to make the development of
other office software more difficult...
Generally I find that LibreOffice files are substantially smaller than Microsoft files.
More recently Microsoft have started to use some open standards and it is possible to
save Microsoft documents in XML.
Getting help The command line
Getting help with Linux questions can be difficult Why use the command line?
so it helps to know where to look. The sites I use ●
How does the command line work?
often are: ●
What is a shell?
●
StackExchange – a network of community- ●
Some simple commands.
managed sites providing expert answers to ●
Files and directories.
questions. ●
Hints and tips for file names.
●
Google – particularly when I have an error ●
Getting help.
message I don't understand.
Surely all that typing can't be right! How can we Case sensitivity
speed things up? ●
Linux commands and filenames are case sensitive:
Filename and command completion: BIG is different from big.
●
<tab> key completes commands and filenames ●
Almost all commands use lower case.
●
In Windows case is not significant. If you save a file as
What else can we do to save typing? big.doc, type in BIG.DOC and Word will find it.
● ●
arrow keys allow us to: However, if you are working with files in both Linux and
Windows you need to take care: Windows will see BIG.DOC
– recall previous commands and big.doc but can only use BIG.DOC.
– change previous commands Guidelines for file names: don't include:
● ●
spaces ● * and ? ● hyphens / and \
The shell is the command that interprets the commands we enter. As you
might expect there are many different shells. We will be using the bash shell.
One significant difference between Windows and Linux is that Linux is case
There are many shortcuts that allow you to use the command line more sensitive. This means that a files called
efficiently. For example, the tab key can be used to complete both commands big
and filenames. If you enter the command Big
BIG
ev are all different files. In Windows this is not so. This difference is important if
you are sharing information between Windows and Linux.
and then press <tab> once you should hear a beep. That's because there is
more than one command beginning with ev. Pressing <tab> twice in quick As we have seen Linux commands use a space as a separator between
succession should display a list of commands beginning with ev . If you then commands and files. This means that spaces in file names can cause
enter the letter i so that you have evi and press <tab> again the command problems. How would you read a file called My File? For similar reasons,
evince should appear. hyphens (-), slashes (/) and backslashes (\) are best avoided in filenames.
Two things Linux does better Exercises for Session 1
Also look out for Firefox and Thunderbird, the browser and mail reader from
the Mozilla stable. Both of these are available from www.mozilla.org and
have versions for Windows as well as Linux. Finally the google chrome
browser is also widely used.
Session 2
You may not realise it, but when you are using the graphical desktop that it
is merely a collection of applications running on top of a shell. Most of the commands available on early Unix systems are still used
today, as well, of course, as many new ones that have since been added.
What we are going to look at today are ways of interacting with shells in a
more direct manner. Unix commands tend to be small and have a single function. This means
that there are a lot of commands to remember. Another side effect of the
long history is that there is generally more than one way to achieve the
end you want. Often there is no correct solution although some are neater
than others.
●
absolute a reference that will work anywhere
/home/ubuntu/Desktop/tmp/newfile
In the above diagram you can see that / is itself a directory (it has to be to more myfile
hold the rest of the file system) and contains a number of directories. I've
marked those typically belonging to the “system” in red and those you often more ./myfile
store files in in blue.
more /export/home/student/username/myfile
The first is a simple path, the second a relative path and the third a direct
path. In the first case you need to be in the same directory to see the file,
in the second you indicate the location in terms of where you currently are
and in the final case you will be able to see the file from wherever you are.
Commands and navigation Finally … some commands
You'll probably be used to navigating though a file system with a You can explore the directory hierarchy using
graphical interface like Internet Explorer. The File Browser serves a
similar purpose. The difference is that IE is regarded as an integral cd ..
part of the Windows OS. The File Browser on the other hand is “just
another application” which you can easily remove and do without. to move up to the next level and
The dependence of Windows on big, complex tools like IE is one of
its major weaknesses. cd
Where UNIX relies on tools (commands) to do things, it demands to return to your home directory. Your home directory is where files
they are small and simple. you create are stored and where you start each new session. Your
home directory will be something like
Note the typically terse names of the Linux commands. They are
usually short for something (pwd is short for print working directory /homes/username
for example). or
/home/ubuntu
… and more ... … and more
The cat command is said to be named after the verb concatenate as it can
also be used to concatenate several files into one. The command displays the
contents of a file on the screen. It is only really useful for small files.
less has all the above options and several more. One useful one is
G move to the end of the file
Other commonly used commands What is a file?
The man command is particularly useful for finding out all the available options
in a file. So A file in Windows is usually (but not always) closely linked to an
application. So a .doc file is considered to be a Word document, a .xls file
man ls an Excel spreadsheet and so on. Although we cannot see it, a lot of
information about the file and the application that runs it is included in the
will give you a vast amount of information on ls including the options. If you file.
are not sure of the name of a command but know what it does then you can
do a keyword search with the apropos command. If you can't remember In Linux, files often contain only plain text. When you look at the contents
what a command does then use of the file you see all there is. Files are not linked so closely to one
application: a file can be viewed, changed and compiled or printed by
whatis ls several different programs.
The commands find and grep are particularly useful. find can search
down through directories looking for files matching a given condition such as
all files changed within the last 7 days. grep will search a given file for a
particular pattern. The two commands are often used in conjunction like this
It has to be said that the syntax is obscure but this command would search all
files changed in the last 7 days containing the string “hello”.
Wild cards and globbing Standard input and output
This slide could be called file name expansion! Linux commands make use of the concepts of
The most commonly used special characters to represent
parts of a filename are: Standard Output (STDOUT)
The use of wildcards is another way that the shell makes specifying
filenames more efficient. So Linux commands can make use of special data streams to move input and
output to and from the command. STDIN can be though of as a gateway into
ls *.txt the code, STDOUT is a gateway for output. Note that there is a third
standard data stream called STDERR (standard error) which commands
will match all files ending in .txt. So important.txt, 123.txt, .txt often use to print error messages and warnings. We won't mention STDERR
would all be matched but data.txt.1 would not. again today. By default STDOUT gets routed to the screen display. It is also
easy to connect a commands STDIN to the keyboard device for example.
ls ?.txt The STDIN allows a program to ask you questions and you can type
responses.
will match all files with a single character before the .txt. So this
would match 1.txt, A.txt, +.txt but not 12.txt and so on.
ls [a-z].txt
will match all files of the form a.txt, b.txt, ... z.txt. Note, too that
wildcards can be used together so
ls [a-z]*.txt
will match all files beginning with a-z and ending with .txt and
containing any characters in between.
Redirection Pipes
Linux can redirect standard output to targets other If commands use STDIN and STDOUT, is it possible to
than the display such as files by using the > character. connect the STDOUT from one command to the STDIN
of another?
YES – use pipes: |
FILE stdout stdout
Command Command Command Command
stdin stdin
Or take input from sources other than the keyboard
by using the < character. A simple example
du -sk /usr/bin/* | sort -n | tail -5
FILE Command
displays the five largest files in the /usr/bin directory
Redirection is a way of “grabbing” STDOUT or STDIN and forcing it to go Connecting commands together with pipes is one of the most powerful
somewhere other than the default. The most common instance is to features of Linux. Linux does not have a command to count the number
redirect STDOUT into a new file. This is extremely useful because it of files in a directory but it does have one command to list the files (ls)
means you can run a command and save the results automatically. To and a second command (wc) to count the number of lines in a list.
redirect STDOUT use the “>” symbol followed by the target so command
> file.txt redirects the output from command into a new file called file.txt. You could therefore use a pipe (|) to glue ls and wc together:
To redirect STDIN into a command use “<” like this: command < file.txt .
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
Exercises for Session 2
●
Using the command line to find your way
around. Session 3
Editors,
●
Using commands to manipulate files. regular expressions,
●
shell scripts
Getting help.
●
Using wildcards to match files and directories.
●
Searching and sorting.
●
Pipes and redirection.
●
Building your own commands.
Controlling the command line
Text Editors
●
Use & to run commands in the background.
Many files in Linux are in plain text format: What happens if you forget?
●
Configuration files. ●
There are control-key commands which allow you to to
●
Shell programs. manipulate the command line:
●
Manual pages (although sometimes stored in CTRL-C interrupt a running program.
compressed format).
CTRL-D send an end of file, ending text input for most
So we need a utility which to let us change text files.
Linux/Unix programs.
●
emacs, xemacs
CTRL-Z suspend a running program.
●
vi, vim
●
gedit, pico, nano
Common to run an editor in the background [demo]
The choice of text editors provokes almost religious feelings among some These are very useful ways of breaking out of a command that looks
Linux users. There isn't a “right” editor to use. Generally a graphical editor like stuck. For a full list see
gedit or emacs is the friendliest to use. Editors like vi are considered much http://web.cecs.pdx.edu/~rootd/catdoc/guide/TheGuide_38.html
less friendly, but are much quicker for small quick changes to a file.
Note that CTRL-C can also be used for copying information. This can
Putting something into “background” means it continues to run, but is hidden cause confusion.
away behind the scenes and is not interactive like a foreground job. Adding an
ampersand after a command makes it run in the background. This is useful for
scripting because otherwise, you would have to close gedit every time you
wanted to stop and try your code out. By putting gedit in background, you can
still use your command line at the same time.
Some editors gedit
vi – visual editor, now often known as vim (vi improved). GNOME Editor is widely available in Linux
●
Very fast, supports regular expressions and a
Simple text editor for the GNOME desktop.
relatively small but powerful command set.
●
● Intuitive – similar to notepad.
Almost all beginners find it difficult to learn.
●
GNU emacs - “extensible, customizable, self- Supports tabs so several files can be edited at
documenting real-time display editor”. once.
● ●
It is an “ integrated environment”: so can do much Context aware – highlights syntax.
else apart from editing.
But if you are familiar with another editor do use
●
It is aware of what sort of file you are editing, so that.
particularly useful for editing programs and scripts.
vi (pronounced vee-eye by purists) can be considered the first real screen gedit is a widely-available and simple-to-use text editor. It has fewer extra
based editor. Before that we used editors which allowed you to alter only a features than emacs and is easier to get started with than vi.
single line at a time. This was very difficult!
It is possible to add plug-ins to increase the flexibility and features available
vi is a “small” editor; that is it has relatively few commands and, in line in gedit. Use
with much Unix philosophy provides a single, specific function. There is no
spell checker or paragraph filler as these features are provided by other Edit -> Preferences -> Plugins
commands.
to find out more.
It is however not particularly intuitive to use and initially needs some effort
to understand.
emacs (properly GNU emacs) is a very powerful editor. What does
powerful mean here? The editor has many features and indeed is
sometimes referred to as an integrated environment rather than just an
editor. As well as editing files it is possible to read and send email and
compile and run programs from within emacs. It is also highly
customizable.
emacs is initially easier to learn than vi but its size and complexity can
mean it is easy to get overwhelmed by choice.
Regular expressions Extended regular expressions
to search for green at the beginning of lines only. and many more.
These are both simple regular expression searches. When using regular expressions, extra options to grep are needed. For
example,
Regular expressions have some similarities with filename wildcards, but
are not the same. Regular expressions are used to find patterns within grep -E '\bgreen\b' fruit
text files, wildcards are used to match filenames.
would find all occurrences of green on its own, but not where it is joined to
another word. So
green cabbage
greengage
would not.
Some limitations
Taking control of the shell
For our exercises today, we don't need to worry about these differences.
Many of the metacharacters and search terms are the same. However, if 'Real World” problems are often too complicated to solve with a single
you are using regular expressions in different applications it's worth command. They also need data to be read and written in a controlled
checking whether there are differences or limitations. way. This session explores some of the ways this can be achieved with
a Linux command line interface.
Note that C# is the Microsoft implementation of ; it provides very good
support for regular expressions.
Scripts Writing scripts
● ●
Rather than typing commands on the command It's usual to write some of the script,
try it out, save a working version Use gedit
line it can be more convenient to put the to create
and add to it. This cycle is repeated. the first
commands into a single file. This is called a version of
the script.
●
Rather than closing your editor Save it.
script.
every time you want to try the script,
● run it in the background with “&”
A script can be run more simply than a series of
Run the
● script
commands which you have to get exactly right For example
every time. gedit &
emacs &
●
You keep control of the command Make
changes
line and fix
errors. Save
script.
In the exercises, we recommend you write a script and try it out until it
Scripts are just plain text files containing commands and optionally, but works. When you're happy with it, save it under a different name (with a
usefully, flow control. Flow control is the means of determining the order in version number on the end for example). This way, if you make a mistake
which commands are executed. in the next exercise, you can go back to the earlier saved version and start
again.
When to script When not to script
● ●
A lot of what you want to do is covered by Your problem can't be broken down into
one or more Linux commands anyway. existing UNIX commands.
● ●
The problem is conceptually simple. You need lots of data/program flow
●
control.
It doesn't have to be blindingly fast.
●
●
It has to be fast.
It's something you have to do a lot.
●
●
It's very numerical.
It's something you want to be automated.
●
It's a one off job.
Once you are familiar with the technique, shell scripts are quick to write. We have already seen that shell scripts can be seen as a quick and dirty
They are often used to avoid tedious and repetitive tasks – for example in solution. With this in mind any problem that requires a carefully designed
the exercises we will write a short script to create several files with similar solution is not a good candidate for a shell script.
names. They are used, too, automate various system administration tasks
that are too trivial to merit a program but for which there is no existing Any application that needs to be fast or involves number crunching or large
command. scale data manipulation should not be solved by a shell script. A program is
needed.
Shell scripts don't require the investment of time and effort that programming
in a higher level language such as C or C++ or Perl or Python. Scripts are Any application that needs a structured solution involving subroutines and
often characterised as “quick and dirty” but there is, I think, a place for this functions is too big a problem for a shell script.
sort of solution.
Executables Exercises for Session 3
●
In Windows .exe files are executable – in ●
Regular expressions
Linux the conventions are much looser. ●
Use the gedit text editor
● ●
In Linux if you write a script you have to Write some shell scripts
manually give people permission to run it.
●
The chmod command changes
permissions on a file:
chmod +x file
allows your script to be executed.
Session 4
●
Using computers remotely.
●
Session 4 We will also look briefly at package
Using computers management and system administration.
remotely
●
Exercises.
The computer we want to use is not always the one in front of us. How
does Linux manage this?
● ●
Often the computer we are sitting at is not the Use
only one we need to use.
ssh
– More powerful
to access remote computers.
– Shared resources ●
Use
– Additional software
scp
●
Set up your Oxford Single Sign-On (SSO) to
access the IT services linux system. to move data.
For users of ARC – the academic research computing facility – and for The ssh command gives us a secure, command line session on a remote
many others, the computer in front of you is not the most powerful or machine.
useful computer available.
In some cases,
We need to find ways to access these remote computers.
ssh -X remote-system
IT services provide a linux commmand line service. The system is running
(at time of writing) Debian 7.11, or the final version of Debian Wheezy. will allow you to open remote graphical applications on your local system.
Although this is a different Linux distribution from the one we’re using, the So for example,
command line interface is generally consistent between distributions.
ssh -X [email protected]
All members of the University can use this system.
then
gedit &
Managing jobs on remote systems
●
Why?
●
Jobs may last longer than a few minutes.
● Managing systems
Don’t want to keep a session on a remote
system: lose connection, lose job.
●
Use
screen
to detach from a session and logout.
Developers cannot predict always what other packages will be used on the
same system as their software; how the system is set up or what version or
distribution is being used.
A means of keeping a system up-to-date is also needed. You could view these
package managers as similar to Windows Install Shield.
Ubuntu Software Updater Other ways of installing software
APT is a rather more flexible and powerful version of RPM and yum. It can Until now we have looked at software that has been easy to download and
also deal with software in different formats. install. We have not had to concern ourselves with the details of what
version we need or where to go to download the software.
I have little experience of setting up an APT system from scratch but this
version of Ubuntu comes configured to run apt successfully. But there will be times when this is not possible so that we will have to find
the software ourselves. It will then need configuring, building and installing
by hand.
One of the many disadvantages with this system is that it is then much
harder to remove the software afterwards as it is not always obvious
whereabouts on the system all the files have been copied.
Managing your system The root account
● ●
Until now we have used the the system to In most Linux systems you will have your own
change our files but not made any system username and password.
changes. ●
Standard user accounts are set up so that you
●
Unlike Windows, Linux has a clear and rigid can change your own home files but not any
distinction between using a system and making system files.
changes to a system, such as installing new ●
To install new software we need an account that
software.
can change files anywhere on the system.
●
So to make changes a special account is ●
This account is known as the root account.
needed.
This “separation of powers” is fundamental to the way Linux (and Unix) Note that because Ubuntu is running from USB separate user accounts are
systems are designed. As you may remember from an earlier session not configured.
you were able to move around the whole system but could not remove
files apart from those in your home directory. To run commands as root you need to use
sudo command
in a command tool Window and press enter. You should now see a different
prompt: a # . This a standard Unix/Linux convention for indicating that this
account is privileged and so dangerous.
Using sudo
Exercises
●
Some systems use a different approach to
We will use the IT services linux service to:
administering system.
●
Use sudo + command when carrying out ●
Log in remotely.
privileged actions. ●
Copy files between systems.
sudo apt-get install bluefish ●
Demonstrate a technique to manage jobs on
will install the bluefish editor. other computers.
●
Normally you will be prompted for a password;
this will be remembered by the system for about
15 minutes.