Department of Computer Science and Software Engineering
CITS4407 Open Source Tools and Scripting
Exercise sheet 1 - sample solutions and discussion
These exercises are performed by entering commands to the
bash
command interpreter (or shell),
an acronym for the Bourne-again shell,
using the
Terminal window application (under either Linux or macOS).
For all of the exercises, use the man command on your system
to find out how to invoke each required command,
and what command-line options it supports.
- What is the current date and time on the computer you're using?
An easy one to start; despite its name, the date
command prints the current date, time, and timezone.
shell> date
Mon Mar 2 10:14:31 AWST 2020
- On what day of the week is the last day of this month?
There's no (easy) single command to find this,
but let's just display the current calendar and read the result.
shell> cal
March 2020
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
- On what day of the week is the last day of this year?
Similarly, the easiest solution is to print the calendar for the
current year, and read the result.
shell> cal -y
2020
.......
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5
4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
25 26 27 28 29 30 31 29 30 27 28 29 30 31
- Each instance of a bash process
has a working directory -
a location in the computer's file system where
files may be read and created (by default).
What is your shell's present working directory?
The Linux manual describes the pwd
command
as 'print name of current/working directory',
but elsewhere it's described as 'present working directory'.
Note that the working directory's full, absolute pathname
is displayed.
Linux uses forward slashes to separate pathname components,
whereas Windows systems use backslashes.
shell> pwd
/Users/chris/cits4407/exercises
- What is the current date and time in London,
which uses Coordinated Universal Time (UTC)?
A simple exercise;
the online manual for date
informs us to use
the '-u' command line option.
Note that your computer hardware maintains the current time in UTC,
and software (such as the date
command)
uses the current timezone to display the more common 'local' time.
shell> date -u
Mon Mar 2 03:13:40 UTC 2020
- .....
Now, use the ls command (that's 'EL' 'S') to list any files and (sub)directories in that working directory.
The output of every ls -l
command will be different,
depending on what's in the current working directory.
Here's an example listing,
showing each file's permissions, links, owner, group, size, modification date,
and filename.
The seond item in the listing is actually a directory, not a file.
shell> ls -l
total 72
-rw-r--r--+ 1 chris admin 2188 Feb 29 05:24 cits4407.css
drwx--x--x+ 13 chris admin 416 Mar 1 17:10 datafiles
-rw-r--r--+ 1 chris admin 5290 Mar 2 11:16 exercise1-soln.php
-rw-r--r--+ 1 chris admin 4008 Mar 1 05:15 exercise1.php
......
- Next, use the mkdir command to make a new directory,
named cits4407, in the present working directory.....
Use the ls command to 'ensure' that the new directory has been created
in the present working directory,
and then ls again (with an argument) to see that the new directory is empty.
shell> mkdir cits4407
shell> ls -l
total 72
drwxr-xr-x+ 2 chris admin 64 Mar 2 11:54 cits4407
-rw-r--r--+ 1 chris admin 2188 Feb 29 05:24 cits4407.css
drwx--x--x+ 13 chris admin 416 Mar 1 17:10 datafiles
-rw-r--r--+ 1 chris admin 5290 Mar 2 11:16 exercise1-soln.php
-rw-r--r--+ 1 chris admin 4008 Mar 1 05:15 exercise1.php
......
shell> ls cits4407
nothing listed inside the new, empty directory.
- Now, change the shell's directory into your new directory named cits4407.
Use ls again to see that it's (still) empty.
shell> cd cits4407
shell> ls -l
still empty.
- Next, create a new file,
named newfile,
by executing touch in this cits4407 directory.
Using the ls command to display a longer, detailed
listing of the contents of the current directory.
shell> touch newfile
shell> ls -l
total 0
-rw--r--r-+ 1 chris admin 0 Mar 2 12:02 newfile
- Now, touch the file newfile again.
What attribute/property of the file has changed?
The file's modification date has changed.
shell> touch newfile
shell> ls -l
total 0
-rw--r--r-+ 1 chris admin 0 Mar 2 12:04 newfile
- OK, let's cleanup this temporary work we've done.
Use the cd command to change back to your home directory.
How do you know you're in the right place?
OK, we've created a new directory named cits4407 and, in it,
a new file named newfile.
Your task is to remove both of them,
so that your home directory is as it was at the start of these exercises.
There's quite a few possible solutions to this.
The important message is that we cannot remove a directory until it is empty.
shell> cd
we are now back in our home directory
remove the new file we created
shell> rm cits4407/newfile
remove the new (now empty) directory
shell> rmdir cits4407
or, remove all files in the directory, then the (now empty) directory
shell> rm cits4407/*
shell> rmdir cits4407
or, recursively remove the whole directory
shell> rm -r cits4407
Chris McDonald
February 2020.