[Next Chapter] [Top page] [Previous Chapter]

CHAPTER 4 - UNIX COMMANDS


Unix commands have the general format:

command  [options]  [item]

Items in brackets are optional, and words in italics are generic identifiers (i.e. options must be replaced by a particular option, e.g. -a).

Note that:

Commands are case sensitive. The command ls is different from LS. In fact LS is not recognised as a valid command.

Command options consist of a single character. The command to list all the files in a directory is ls -a and could not be ls -all (the latter would have to mean a combination of options.)

Command options can usually be combined or listed separately. For example:

ls -al or ls -a -l

The command item is given last. This is very often a file name. For example:

ls -a file1.f		not		ls file1.f -a

The echo command

The echo command 'echoes' its argument to the standard output. This means that in its simplest form it prints something out on screen. For example:

% echo Hello				- you type
Hello					- response from the shell%

Who is logged on?

The command who gives a list of logged on users:

% who
root     console Jan  4 10:34
men6matw ttyp1   Jan  6 09:45  (ecusun1)
cbl6nd   ttyp2   Jan  6 10:10  (cblslcd)
cbl6ar   ttyp3   Jan  6 16:03  (cblsuna)
csc6ea   ttyp4   Jan  6 14:15  (csuna1)
root     ttyp5   Jan  6 10:40  (sun032)
ecl6rsh  ttyp6   Jan  6 15:39
csc6ea   ttyp8   Jan  6 14:15  (csuna1)
lnp5mw   ttyUf   Jan  6 16:16
lnp5jb   ttyp3   Jan  6 15:20  (sun051)

Also try the command finger. This command gives the full name of logged in users.


PRACTICE

Type finger to get information on yourself and other users.


Creating a directory

The mkdir command is used to create directories. The format of this command is:
% mkdir directory_name

Jenny Brown stores her unix scripts in a directory called scripts beneath her home directory. In order to create this directory she uses the command:

% mkdir scripts

Deleting a directory

The rmdir command is used to delete directories. The format of this command is:
% rmdir directory_name

Jenny Brown stores files for project work in a directory called proj. When the project has been completed she deletes the directory using the command:

% rmdir proj

Note that the directory must be empty before it can be deleted.

Listing contents of a directory

The command ls is used to list the contents of a directory. For example:
% ls
file1   scripts    test.f     test

Notice that directories are listed as well as files. To list all files, including hidden files, give the command:

% ls -a
.cshrc    file1    bin    test.f    test

Hidden files begin with . (a full stop). Hidden files are normally system files, and will normally include the following:

% ls -a
.cshrc   .forward   .history   .login   .logout

The purpose of some hidden files.

To identify directories in a listing give the command:

% ls -F
file1     bin/     test.f     test

Notice how the directory is identified by the slash (/) character.

Deleting files

Files can be deleted using the rm command. For example:

% rm test.f

Displaying files

The command cat is used to display the contents of a file on the screen.

For example:

% cat file1

Creating files

The command cat can also be used to create a file. For example:

%  cat  >  test.f
When typing in a new file
the input must be terminated by
^D

NOTE ^D means press the <ctrl> and the d keys simultaneously. Be careful not to type ^D when you have the shell prompt, because this might log you out. Normally you would use an editor for creating files. This example is given since it illustrates how to create a small file without needing to learn the use of an editor.

Copying files

The command cp is used to copy a file. It takes the format:

%  cp  old_file  new_file

For example:

%  cp  file1  file2

Renaming files

The command mv is used to rename a file.

For example:

%  mv  file2  temp

changes the name of file2 to temp.

Moving files

The command mv is also used to move a file to a new location in the filestore hierarchy. For example:

% mv file2 bin

moves the file file2 into the subdirectory bin.

Overwriting files

Commands such as rm and cp can be dangerous if not used with care. The command:

%  cp  file1  file2

will delete file2 if a file of that name already exists. If you have spelled the name of the new file incorrectly you may accidentally overwrite the contents of a file. Using the wildcard symbol * with the command rm can also be very dangerous. The command:

% rm test*

will delete all files starting with test. However if you inadvertently type an extra space (do not try this!):

% rm test *				-do not try this!

the file test will be deleted if it exists. Then all other files in the directory will be deleted! Often no warning will be given.

To prevent accidental deletion of files you can use the -i option with commands such as rm. The format of the command is:

% rm -i file

You will be asked to confirm that files are to be deleted. You may find that this is set as the default on your system.

Wildcards

Wildcard characters can be used to identify directory and file names. The wildcard character * is used to refer to any combination of characters. For example:

%  ls  *	- refers to all files
% cat test*	- refers to all files starting with 'test',  
		  e.g. 'test', 'testing', 'test.c', etc.

The wildcard character ? is used to refer to a single character. For example:

% ls test?	- refers to files starting with 'test' followed by a single				character  e.g. 'test1', 'test2', 'testz', etc.% cat test.?	- refers to all files starting with 'test' with a single character				after the full stop, e.g. 'test.c, test.f'

Exercises

  1. Display your current working directory using the pwd command.
  2. Make a directory called exercises.
  3. Change your directory to the directory exercises. Display the current working directory.
  4. Return to your home directory.
  5. List the contents of your directory. Use the -l, -a and -F options and compare the output.
  6. Change your directory to the directory exercises. Create a file called example1 using the cat command containing the following text:
    water, water everywhere
    and all the boards did shrink;
    water, water everywhere,
    Nor drop to drink
    
  7. List the contents of your directory. Use the -l option to obtain a long listing.

[Next Chapter] [Top page] [Previous Chapter]