Go into Dat_F and make another new directory
called
Exercise_1 and go into this
directory. We are now going to make some exercises on file handling here to make
you confident with several of the other commands that were introduced at the
lecture.
Create an empty file by typing:
$
touch test_file
touch is a command that can either be used to
create an empty file or if used on existing files change the time at which they
were last updated. Confirm that the file is there using
ls -l, which shows something like
this:
-rw-r--r-- 1
kg users 0 Jun 28 15:51
test_file
The
red
shows the permissions on the file. The first three entries (rw-) shows that the
owner of the file,
kg,
is allowed to read and write the file. The next three entries (r--) show that
users belonging to the same
group are
allowed to read the file. Finally, the last three entries (r--) implies that all
other
users on the system are also allowed to read the file. This is the default
permission on files on this system. Other system administrators may have chosen
a different setup where only the user is allowed to read the file.). You can
easily change these permissions. Assume you have a file or directory that you
don't want anyone else to access. Then you have to change the permission for
group
and
other This
is done in the following way:
$
chmod g-r test_file-rw----r-- 1 kg
users 0 Jun 28 15:51 test_file
as it can be seen, the read permission has
now been removed for
group. The same can be
done for
other and
usr,
using
u or
o instead of
g in the expression above. They
may also be combined to change more than one flag at a time. The third entry "-"
after
w stands for execute, implying
that the file contains some form of a program or script that can be executed by
typing the files name from the prompt in the command tool. You will find that
the compiled Fortran programs will contain this permission.
(There
is more than one way to do this. If you have tried it on a file and it works and
you get a negative reply here. I may have missed that combination in the list of
answers. Let me know and I will incl it.)
Disk usage:As there
are many users on the system, we are limited in the amount of disk space each of
us are allowed to use. This restriction is enforced automatically on the home
directory making sure the disk is not filled up by a single user making some
less lucky actions. There are a number of commands used for checking both global
and local disk usages.
$
df
gives a listing of all the file systems associated with the computer you
are logged on to. Various options can be used to make the output more
readable.
There are three different definitions of a Megabyte. To clearfy the
differences take a look at this webpage.
To see how much disk space you are using use the
command quota.
$ quota
quota shows both your present disk usage and
how much disk space you are allowed to use. Are you close to your limit? I guess
not! There are two important limits. The first is named quota and the
other limit. The first one tells how much disk space you are allowed to
use without being told by the system. When you pass this limit, you will be told
- either next time you login or next time you open a new command tool. You are
then given a finite time to remove enough data to bring you below this limit.
The limit is a more severe limit. If you reach this one, you can't
continue working before you have removed enough data to bring you below the
quota limit. One effect of this is that you can't login to the machine
using a graphical terminal as this process writes information to your home
directory. As a student you are not given a very high quota, so as we proceed
with the course, you may find that you have to clean out data that can easily be
reproduced.
Assume you are close to you quota limit, and you need
to clean up by deleting files. If you have a large directory tree, the previous
commands don't tell you where you use most disk space. This may be achieved
using the du command. Using specific
options with du it possible to get
information about the distribution of disk usage in your files/directories below
the pwd.
$ du ?? *
Wild cards:
At the
end of the previous section we use a * to represent all entries in the directory
when executing the
du command. In Linux
there are a number of possibilities to use wild cards. These can be used to
substitute either one or more letters in the argument to a command. This makes
it easy to select a number of files/directories to act on.
- * is used to represent
a long string in a name: This_is_a_long_name could possible be picked
up as This*
- ? is used to substitute a
single character: ls -l test1
test2 test3 test4 may be written as ls -l test?
Notice that the wild
card can also pickup unwanted files, so be careful when using them in
destructive actions!
Just to try it out try
doing:
$ touch
test{1,2,3,4,5,6} log $ ls
t* test?
Notice how the "{1,2,3,4,5,6}" syntax is use to create
several files with very similar names. Now remove these files using the command
rm.
Process
handling:It is useful to be able to see which process
runs on the computer, and especially which process you have running. There are
different ways to do this, the most used is the command
ps.. Typing
$
ps
simply gives a listing of your own
process started from this command tool giving something like this:
PID TTY TIME CMD
21847
pts/6 00:00:00 tcsh
24483 pts/6 00:00:00
ps
The
PID is the process
identification number, such a number is given to each process that runs on the
computer.
Time indicates how much CPU
time the process has required. There are many options to
ps, the most used is possible
$
ps -ef
that is used to give a long
listing of all process
UID PID PPID
C STIME TTY
TIME CMD
root 1
0 0 Jun02
? 00:00:01 init [5]
root 2
1 0 Jun02
? 00:00:00
[migration/0]
root 3
1 0 Jun02
? 00:00:00
[ksoftirqd/0]
root 4
1 0 Jun02
? 00:00:00
[migration/1]
.....
kg
21845 21843 0 12:53
? 00:00:00 sshd:
kg@pts/6
kg 21847
21845 0 12:53 pts/6 00:00:00
-tcsh
root 24044
2721 0 17:55 ?
00:00:00 sshd: romeo [priv]
romeo 24046
24044 0 17:55 ?
00:00:00 sshd: romeo@pts/7
romeo 24047
24046 0 17:55 pts/7 00:00:00
-tcsh
kg 24544 21847
0 18:45 pts/6 00:00:00 ps
-ef
You can use
ps with one option to show all your processes
on the machine.
A different way to see the running processes is by using the top command:
$ top
This gives a listing of processes,
with the most CPU demanding at the top. This list is per default updated every 5
seconds.
To get out of top you have to type q inside the window.
Editors:
In the
following exercises you are going to make much use of an editor for writing
codes etc. It is therefore important that you get familiar with one right from
the start. On all Linux systems there are a number of different editors
available:
The first
five are simple full screen editors that are straight forward to use. Most of
the functionality is provided through menus at the top of the editor image.
vim is an older Unix line editor, which
is more complicated to use as it requires knowledge to a number of shortcuts.
But when known, it is able to do much more sophisticated operations than the
simpler editors.
emacs represents
another standard editor that in the latest versions can do almost everything you
may need to do. The problem, as with vim, is that it takes some effort to become
acquainted with all its functionality.
If you don't have a favorite
editor already, I would suggest you to start with one of the simpler editors.
Try them out and see which one suits you best. These editors are found on most
Linux distributions today.
To test the functionality of the editors,
download this
file to the disk
(use shift when clicking on the link) and do the changes suggested below to get
a feeling of how the different editors are handling various common editor
operations.
- Make a backup copy of the file before start plying with it.
- Move section 1 below the present section 2.
- Move the 3rd word in line 10 to position 8th in the same line.
- Move the 3rd to 5th words in line 15 to the previous position 8th in the
same line.
- Replace the work ddxdn with dummy1 everywhere.
- Correct the spelling of the word compuuter everywhere.
- ...
- Copy the backup file to the text_file and redo the changes using a
different editor.
Pipe and
redirect:
Until now we have only been dealing with
moving files around and changing the permissions on them. There are situations
where it is required to get the content from one file and feed it into a program
which them does something depending on the input. One example could be to simply
count the number of lines and words in files. Another could be a program that
produces data written to the screen. For some reason you may want to store this
in a file for later analysis. There is a simple way to handle this situation
using the following symbolic representation for the data
control:
$ cat
file
reads the content of file to the display. Assume we want to get the
content from file into another file
called tmp. This may be obtained by the
following command:
$ cat file >
tmp
where > grabs the output from cat and puts it into tmp. A more general approach to this is to use
the command cp.
Just to read the content of a file without opening it in an editor you should
try more file and possible also the related tail....
The
program wc counts the number of lines
and words in its input. To count the lines of file above you could do
$
cat file | wc
The symbol | implies that the output
from the first command is used as input for the following
command.
Additional we may use >>, which implies that the content
of file is added at the end of the existing file, tmp
$
cat file >> tmp
Similarly
< can be used to redirect input into a program, for instance as shown
here,
$ wc < file >
tmp
This line counts the number of lines and words in file and puts the result into tmp.
Working
environment:
In your work directory there are a number
of hidden files, meaning that they do not show up in a normal listing by ls. The names of these files all begin with a
"." and can be seen using ls -a. Typing
this command, you notice that all directories contain a "." and a ".." entry.
These relate to the present directory (.) and the directory one above (..). So
by using cd ., you stay where you are,
while cd .. implies that you go one
directory level up.
In your home directory there are many .name files.
These are used by various programs to save temporary information. But there are
also two files that have implication on what happens when you first login and
each time a new process is started. These are the .login and .cshrc files. If you want your environment to
behave in a particular way, then this is where you define it. The only thing
that is done right now is to source the appropriate standard files that are
active for all users and may be something particular from a previous course.
Connecting to a different computer:
You are now, most likely, logged in on one of the
SUNray terminals at the RF building. But you may also have a
user account on other machines (A few years back you would have individual
accounts at Physics, Geophysics and Astronomy) or need access to a different
machine in the same network to do something special. There exist various
protocols that allows you to login from one machine to another. A general
concern here is the security in the protocol that handles this connection. Today
there is only one mechanism that allows you to do this in a secure why. Namely
ssh, which is short for secure shell. To login to HCØ from a different
machine (the one you have at home or lynx) you will have to use this command
from a command shell:
add Title, Labels, grid.... and replot