Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Page 1 of 10

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

1. Linux Tour – Ubuntu

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.

Open a web browser, and go to https://ubuntu.com/desktop/features

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.

CO1508 Computer Systems and Security, UCLAN – 2019-2020


Page 2 of 10

Open a web browser and go to http://webminal.org


You will need to create an account with the website (click where it says ‘Register’), then
when you are logged in you can access the remote machine.

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

To list folder contents, you can use ls

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

CO1508 Computer Systems and Security, UCLAN – 2019-2020


Page 3 of 10

$ls
It’s empty because it’s a new directory.

Let’s create a new empty file.


$touch file1
$ls
$ls -l
What does -l do when used with ls? (Don’t worry, we’ll talk about permissions later)
If you ever want help with any Linux command, just type man command. For example:
$man ls
To exit, press q

3. Linux Terminal – nano

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:

Roses are red,


Violets are blue,
Linux is brilliant,
I know it’s true.
When you are done, you can exit the file – the commands to do so are shown at the bottom
of the screen (the ‘carat’ ^ indicates ‘ctrl’, so for example ^X to exit means press ctrl-x).
You’ll be asked if you want to save the file, type Y. Then, you’ll be asked if you want to keep
the file name file1. Just click Enter.
You should now return to the command line.

CO1508 Computer Systems and Security, UCLAN – 2019-2020


Page 4 of 10

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.

To check the properties of file1


$ls -l
You should see a date and time for the file you created. Is this the time the file was created,
accessed, or modified?

4. Linux Terminal – File Permissions

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:

CO1508 Computer Systems and Security, UCLAN – 2019-2020


Page 5 of 10

-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

We can use chmod command in two ways:


• We can set permissions for user, group, other and all. For example:
- chmod u=rwx (permissions for the user set to read, write and execute)
- chmod a=r (permissions for all set to read only)
- chmod ug=rw (permissions for the user and group set to read and write)
- chmod o=r (permissions for others set to read only)

• We can also add or remove permissions for certain groups, e.g.:


- chmod o+w (add write permissions for others)
- chmod a-r (remove read permissions for all)

CO1508 Computer Systems and Security, UCLAN – 2019-2020


Page 6 of 10

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 and executable for your group

• 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:

# Permission rwx Binary


7 read, write and execute rwx 111
6 read and write rw- 110
5 read and execute r-x 101
4 read only r-- 100
3 write and execute -wx 011
2 write only -w- 010
1 execute only --x 001
0 none --- 000

For example, 754 would allow:


• read, write, and execute for the user, as the binary value of 7 is 111, meaning all bits are
on.
• read and execute for the Group, as the binary value of 5 is 101, meaning read and
execute are on but write is off.
• read only for others, as the binary value of 4 is 100, meaning that only read is on.

CO1508 Computer Systems and Security, UCLAN – 2019-2020


Page 7 of 10

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)

5. Linux Terminal – Wildcards

We looked at wildcards before in Windows. Now, we’ll use the same tricks in Linux.

Try to list the contents of /usr/bin directory as follows:


$ls /usr/bin

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.

To list everything that starts with ‘a’, try this:


$ls /usr/bin/a*

Now try:
$ls /usr/bin/A*

What happened?

Now try this:


$ls /usr/bin/*a*
This will list everything that has ‘a’ in its name.

Now try these commands and explain what happens for each:
$ls /usr/bin/[a-c]*

CO1508 Computer Systems and Security, UCLAN – 2019-2020


Page 8 of 10

$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*

6. Linux Terminal – Looking inside files

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!

CO1508 Computer Systems and Security, UCLAN – 2019-2020


Page 9 of 10

7. Linux Terminal – Copying, Moving, Deleting Files and Directories

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 copy a file, use the command cp


Assuming that you’re in CO1508_9 directory and have file1 there. Try the following:
$cp file1 file2
$ls

To move or rename a file, use the command mv


The following command will rename file2 to file22
$mv file2 file22
$ls

Now, let’s move file22 to your home folder:


$mv file22 ~
$ls
$ls ~

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.

CO1508 Computer Systems and Security, UCLAN – 2019-2020


Page 10 of 10

$rm CO1508_9/*
$rmdir CO1508_9

8. Linux Terminal – Your Task

To make sure you’ve understood the commands above, do the following tasks and write the
command for each request:

• List all the files in /etc

• Create a new folder called CO1508_9 in your home directory

• Inside CO1508_9, create a text file called myfile.txt and write some random text into it

• Copy myfile.txt into a new file called otherfile.txt

• Search inside both files for every word that starts with the ‘a’ (assuming you wrote
something that starts with ‘a’)

• Rename otherfile.txt to myfile2.txt

• Change the permissions for myfile.txt to be read-only for everyone

• Change the permissions for myfile2.txt to be read, write for yourself and your group and
executable-only for others

• Finally, delete CO1508_9 directory

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

CO1508 Computer Systems and Security, UCLAN – 2019-2020

You might also like