next up previous contents
Next: UNIX Up: File Basics Previous: Basic UNIX and DOS   Contents

Putting Theory into Practice in UNIX

So far this module has been rather frustrating, all theory and no practice, so now it is time to try some of this out. Log onto PPLXINTnn and then type:-
LS
this will not work - UNIX is case sensitive so all commands should be entered in lower case. Now try:-
ls
It will list all files on disk in your top directory that belong to you. Type:-
cat > text.txt
This is a sample text file
It contains two lines
and then press Ctrl D. By default cat sends its output to the screen but the closing angle bracket followed by a file name can be used to redirect its output (or any other UNIX command) to the named file instead. Now try:-
ls -l test.txt
the -l tells ls to give more details, and by giving a file name ls only gives information about it rather than all the files in the directory. The output will look rather like this:-
-rw-r-r- 1 smith users 49 Sep 15 11:17 text.txt
where:-
-rw-r-r-
is the file access modes, we won't deal with this here.
smith
is the your user name
users
is the group you belong to
49
is the number of bytes in the file
Sep 15 11:17
is the date it was last modified
To see what the file contains, type it out using the cat (concatenate) command:-
cat text.txt
The cat command is useful for displaying short files, this particular file should contain the two lines above. To make a copy of the file, type:-
cp text.txt fred.txt
You will now have a fred.txt (check this by using ls). It is an exact copy of the text.txt file so should also contain 2 lines. You can check this by typing:-
cat fred.txt
To get rid of a file the rm (remove) command is used. Try:-
rm fred.txt
UNIX does not have automatic command prompting, but it does show the usage for the command. To demonstrate this type:-
cp
To rename a file use the mv (move) command e.g.:-
mv test.txt fred.txt
It is rather like copy except that the old version of the file disappears.

Another useful basic file handling command is lpr which prints the file on the default printer. However this is only useful when:-

On UNIX the full file specification has the following form:-
directory-path/file-name

There are only two components: the directory path and the file name. The file name have does not have an extension as such (unlike DOS). However the character '.' is permitted as part of the file name so it is possible to have file names like test.dat and some software assumes that files do take this form. Special files, usually configuration files, start with an a dot (.). Such files are "hidden" i.e. they are not displayed when you list out files unless you use the -a option to the ls command.

Although all the files we have seen so far are on disk, other devices can be used to hold files. On DOS the file specification starts with a device name that ends with a colon(:). In most cases only the device is specified - other components of the file specification may be meaningless. On UNIX, devices appear to be files in the special directory /dev, for example:-


/dev/tty 		  the current user's terminal

/dev/nrmt2h a magnetic tape drive
/dev/null null device

To show a simple example of a non-disk device used in a command type:-

cat text.txt > /dev/tty
this copies test.txt to the current user's terminal i.e. it types it out. In this way a single file specification system can be used to describe the storage and movement of data on any device.

Finally we shall play with directories. Start by typing:-

pwd
pwd (present working directory) will show you what your current (working) directory is. The directory name will look something like this:-
/home/smith
assuming your name is Smith! In the examples that follow, wherever you see smith, replace it with your username. Now type:-
mkdir test
this mkdir (make directory) command will create the directory test in your main directory. Now type:-
cp test.txt test/fred.txt
This will copy test.txt to your test directory where the file will be called fred.txt. Notice how the input file had no directory name so the default directory was used and the output used relative directory addressing. If you now type:-
ls -l
you will see that you have a entry like this:-
drwxr-xr-x 2 smith users 512 Sep 15 11:25 test
in your main directory. The d at the start of the line means that it is a directory. To see what it contains type:-
ls /home/smith/test
This time we used absolute directory addressing. You should see the fred.txt Now type:-
cd test
ls -l
to switch default directory to /home/smith/test and then see what is in the current directory. Now type:-
ls ../
which uses relative directory addressing to access your main directory.

Before you can delete a directory you must first empty it. So delete the file fred.txt now. Then move back to your top level directory and delete the test directory, which you do with the rmdir (remove directory):-

rm fred.txt
cd ../
rmdir test


next up previous contents
Next: UNIX Up: File Basics Previous: Basic UNIX and DOS   Contents
P.D. Gronbech (IT Staff) 2015-10-02