Professional Documents
Culture Documents
CO1508 Computer Systems & Security - Week 14 - Linux Tour, Basic Commands and Text Editing
CO1508 Computer Systems & Security - Week 14 - Linux Tour, Basic Commands and Text Editing
Summary
You are going to explore Linux Operating System and learn how to use Linux terminal for
basic commands such as exploring directories, making files, deleting folders, etc.
Activities
Ubuntu is a commonly used distribution of Linux. This is a free operating system that anyone
can download and run on their computer. It will even boot from a cd or USB stick.
Ubuntu is designed so that the graphical interface will look very similar to Windows, even if
the underlying operating system works very differently. First, let’s have a look at the GUI
version of the system.
Have a look at these features and compare with Windows. See what looks similar and what
looks different from Windows.
(The OS is free, you can follow the instructions on their website to install it on a cd or USB
stick if you like, so you can try it more for yourself. However, you can do that at home in
your own time.)
2. Linux Terminal
Ok, now for the real work. We are going to log into a remotely located machine, which
happens to be running Linux, and interact with it from our own machines.
There are lots of instructions to follow here, but if there is anything you don’t understand or
that doesn’t seem to work, discuss it with your lab tutor.
Click on the ‘Terminal’ link in the menu on the webminal page. You can now log in to the
remote machine using your username and password. When you have done so, you should
see a welcome message appear, and then the command prompt.
What symbol is used for the Linux prompt?
(Hint: in Windows it was > )
The path may also look a little different to the Windows path. The ‘tilda’ symbol (~) is used
to indicate you are in your ‘home’ folder. If you want to see the full path for your current
directory, use the command pwd.
What is the full path to your current directory?
You can change directories using the cd command with a path. If you want to move up one
folder from where you are, you can use cd .. or to move back to your home folder you
can just type cd ~ or just cd
Go to home directory $cd home and list its contents $ls. It may take a while! What are
you seeing here? Can you open any of these folders? You can stop it by pressing Ctrl + C
Let's create a new directory in your home folder to do some work in.
$cd
$mkdir CO1508_9
$ls (to make sure it’s created)
Now let’s move into the new directory and try some commands
$cd CO1508_9
$pwd
$ls
It’s empty because it’s a new directory.
Make sure you are in your home folder ~ It’s time to create a text file using the text editor
nano. Type the following commands:
$cd CO1508_9
$nano file1
Nano is a simple text editor, just like Notepad, except you can use it from the command line.
Type the following into file1:
If you list the contents of your CO1508_9 folder now, you should see the file you created.
(You can read the contents and edit it again by typing the name of the file after the
application name – i.e. $nano file1)
Open file1 again using nano and cut the line “Linux is brilliant” and paste it back three times
so the text looks like:
Roses are red,
Violets are blue,
Linux is brilliant,
Linux is brilliant,
Linux is brilliant,
I know it’s true.
Save the file and exit nano.
We are now going to look at file permissions, which can get a little complicated. However, if
you’ve understood everything that we have covered so far then you can get this one too.
Listing the directory contents in the long listing format $ls -l will show the permissions
for the file. There are 10 characters:
• The first is a ‘d’ if the file is a directory, or else is blank (-).
• The next characters are in groups of three, representing the permissions for the user
(the owner of the file), the group (that the owner is part of), and all others. The options
are ‘r’ for ‘read’, ‘w’ for ‘write’ and ‘x’ for ‘execute’.
So, for a file that the owner can read, write and execute, but all others can only read, it will
look like this:
-rwxr--r--
A file that everyone can read, write, and execute will look like this:
-rwxrwxrwx
A directory that can only be read and executed will look like this:
dr-xr-xr-x
Look at the file permissions for the file you created, and check you understand what
permissions are available, and who has them.
To make things easier, here’s a table to break everything down for you (note this is my file1
when I created the lab sheet ☺):
Type User Group Others No. of links Owner Owner Group Size Last modification Name
time
- rw- rw- r-- 1 yours Yours 15 24th Feb 14:26 File1
The command we use to change the permissions (or mode) for files is called chmod. Try this
and read the information on chmod command:
$man chmod
Now, try chmod command and change the permissions for the file you have created (file1)
as follows:
• Make it executable for yourself only
• Make it readable only for everyone (i.e., remove the executable permission)
• Make the file inaccessible for everyone (i.e., remove all permissions)
Now for the hard part. You can also specify the file permissions in a numerical format. This
uses octal, or base 8. Have a look at this table:
Now try it yourself and change the permissions of file1 to allow yourself (the user) and your
group to read and write only while others are only allowed to read.
(If you’re stuck here, ask your tutor for help before moving to the next section)
We looked at wildcards before in Windows. Now, we’ll use the same tricks in Linux.
It looks a big list. Remember the pipe symbol we used in windows? Try it here now as
follows:
$ls /usr/bin | less
$ls /usr/bin | more
less or more would do the trick for you. Note: press q to quit the listing at any time.
Now try:
$ls /usr/bin/A*
What happened?
Now try these commands and explain what happens for each:
$ls /usr/bin/[a-c]*
$ls /usr/bin/[a-c]*f*
$ls /usr/bin/*[0-9]*
As well as “*” there is another wild card “?”. This means “anything which is one character
long”. Try the following and explain what happens for each:
$ls /usr/bin/?a*
$ls /usr/bin/*a?
$ls /usr/bin/??a*
Now, go back to your folder CO1508_9. We had one file there called file1. Let’s have a look
inside:
$cat file1
cat will show you the contents of the file on screen. The problem with cat is big files
rapidly scroll off the top of the terminal screen. Hence, we can use less for big files:
$less file1
(press q to exit or space to scroll in case of a big file) Try it with this:
$less /etc/nfs.conf
Do you want to search for a specific word inside a file? No problem. Try this:
$grep brilliant file1
(assuming you’ve written the word brilliant in file1).
Try it again:
$grep port /etc/nfs.conf
It’ll show you the lines where the word ‘port’ is mentioned. grep is a very useful command.
Keep it in mind!
You learnt earlier how to make a directory (i.e., folder) using mkdir. Also, you learnt
touch to make a file. nano can also be used to create a file when you enter the text editor
and save changes to a new file.
To delete a file, use the command rm the following command will delete file22 from the
home folder:
$rm ~/file22
Did you notice that there’s no warning whatsoever? Linux assumes you know what you’re
doing! So be careful.
To delete a folder, use the command rmdir the following command will delete CO1508_9
directory but first, you should be outside it.
$cd ~
$rmdir CO1508_9
It’ll fail because CO1508_9 is not an empty directory! So, you’ve to delete everything inside
it first.
$rm CO1508_9/*
$rmdir CO1508_9
To make sure you’ve understood the commands above, do the following tasks and write the
command for each request:
• Inside CO1508_9, create a text file called myfile.txt and write some random text into it
• Search inside both files for every word that starts with the ‘a’ (assuming you wrote
something that starts with ‘a’)
• Change the permissions for myfile2.txt to be read, write for yourself and your group and
executable-only for others
Show your tutor your answers. Finish off by typing exit to close the terminal and log out.
9. Coursework 2
Are you done? Spend the last few minutes of your lab session to continue working on your
coursework.atch script solution