Exercise 1.listing Files and Directories: Command Meaning Ls Ls - A Mkdir CD Directory CD CD CD .
Exercise 1.listing Files and Directories: Command Meaning Ls Ls - A Mkdir CD Directory CD CD CD .
Command Meaning
cd change to home-directory
cd ~ change to home-directory
Exercise 2a
Exercise 2b
Create a directory called tempstuff using mkdir , then remove it using the rmdir
command.
cp (copy)
cp file1 file2 is the command which makes a copy of file1 in the current working
directory and calls it file2
mv (move)
To delete (remove) a file, use the rm command. As an example, we are going to create a
copy of the science.txt file then delete it.
% cp science.txt tempfile.txt
% ls
% rm tempfile.txt
% ls
You can use the rmdir command to remove a directory (make sure it is empty first). Try to
remove the backups directory. You will not be able to since UNIX will not let you remove a
non-empty directory.
Before you start the next section, you may like to clear the terminal window of the previous
commands so the output of the following commands can be clearly understood.
% clear
This will clear all text and leave you with the % prompt at the top of the window.
cat (concatenate)
The command cat can be used to display the contents of a file on the screen. Type:
% cat science.txt
As you can see, the file is longer than than the size of the window, so it scrolls past making
it unreadable.
less
The command less writes the contents of a file onto the screen a page at a time. Type
% less science.txt
Press the [space-bar] if you want to see another page, and type [q] if you want to quit
reading. As you can see, less is used in preference to cat for long files.
head
The head command writes the first ten lines of a file to the screen.
% head science.txt
Then type
% head -5 science.txt
tail
The tail command writes the last ten lines of a file to the screen.
% tail science.txt
grep is one of many standard UNIX utilities. It searches files for specified words or patterns.
First clear the screen, then type
% grep science science.txt
As you can see, grep has printed out each line containg the word science.
Or has it ????
Try typing
The grep command is case sensitive; it distinguishes between Science and science.
To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe
symbol). For example to search for spinning top, type
Try some of them and see the different results. Don't forget, you can use more than one
option at a time. For example, the number of lines without the words science or Science is
Command Meaning
Using the above method, create another file called list2 containing the following fruit:
orange, plum, mango, grapefruit. Read the contents of list2
We use the > symbol to redirect the output of a command. For example, to create a file
called list1 containing a list of fruit, type
Then type in the names of some fruit. Press [Return] after each one.
pear
banana
apple
^D {this means press [Ctrl] and [d] to stop}
What happens is the cat command reads the standard input (the keyboard) and the >
redirects the output, which normally goes to the screen, into a file called list1
% cat list1
Appending to a file
The form >> appends standard output to a file. So to add more items to the file list1, type
peach
grape
orange
^D (Control D to stop)
% cat list1
You should now have two files. One contains six fruit, the other contains four fruit.
We will now use the cat command to join (concatenate) list1 and list2 into a new file called
biglist. Type
Using pipes, display all lines of list1 and list2 containing the letter 'p', and sort the result.
Answer
Wildcards
The * wildcard
The character * is called a wildcard, and will match against none or more character(s) in a
file (or directory) name. For example, in your unixstuff directory, type
% ls list*
This will list all files in the current directory starting with list....
Try typing
% ls *list
This will list all files in the current directory ending with ....list
The ? wildcard
So ?ouse will match files like house and mouse, but not grouse.
Try typing
% ls ?list
Filename conventions
We should note here that a directory is merely a special type of file. So the rules and
conventions for naming files apply also to directories.
In naming files, characters with special meanings such as / * & % , should be avoided.
Also, avoid using spaces within names. The safest way to name a file is to use only
alphanumeric characters, that is, letters and numbers, together with _ (underscore) and .
(dot).
project.txt project
File names conventionally start with a lower-case letter, and may end with a dot followed by
a group of letters indicating the contents of the file. For example, all files consisting of C
code may be named with the ending .c, for example, prog1.c . Then in order to list all files
containing C code in your home directory, you need only type ls *.c in that directory.
Exercise 6a
Try changing access permissions on the file science.txt and on the directory backups
You will see that you now get lots of details about the contents of your directory, similar to
the example below.
Each file (and directory) has associated access rights, which may be found by typing ls -l.
Also, ls -lg gives additional information as to which group owns the file (beng95 in the
following example):
-rwxrw-r-- 1 ee51ab beng95 2450 Sept29 11:52 file1
The 9 remaining symbols indicate the permissions, or access rights, and are taken as three
groups of 3.
The left group of 3 gives the file permissions for the user that owns the file (or directory)
(ee51ab in the above example);
the middle group gives the permissions for the group of people to whom the file (or directory)
belongs (eebeng95 in the above example);
the rightmost group gives the permissions for all others.
The symbols r, w, etc., have slightly different meanings depending on whether they refer to
a simple file or to a directory.
r (or -), indicates read permission (or otherwise), that is, the presence or absence of
permission to read and copy the file
w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise) to
change a file
x (or -), indicates execution permission (or otherwise), that is, the permission to execute a
file, where appropriate
So, in order to read a file, you must have execute permission on the directory containing
that file, and hence on any directory containing that directory as a subdirectory, and so on,
up the tree.
Some examples
-rwxrwxrwx a file that everyone can read, write and execute (and delete).
a file that only the owner can read and write - no-one else
-rw------- can read or write and no-one has execution rights (e.g. your
mailbox file).
Only the owner of a file can use chmod to change the permissions of a file. The options of
chmod are as follows
Symbol Meaning
u user
g group
o other
a all
r read
+ add permission
For example, to remove read write and execute permissions on the file biglist for the group
and others, type