Tutorial: Basic Unix continued
1.1 Input/Output redirection: Redirection to a file
Instead of sending output from a command to the screen, it can be send to a file.
This is done by using ">" (which creates a new file or overwrites an existing
file) or by using ">>" (which appends to an existing file).
- Type the following commands:
cal 3 1991
cal > file1
cal 3 1991 >> file1
- Concatenate 2 files into one file using "cat" and file redirection.
A command can also read input from a file, using "<". The following
two lines are equivalent:
more < file1
- Use file redirection to create a file named 'yearfile' in your directory.
Put a whole year's calendar into it. Then append the next year's calendar.
1.2 Input/Output redirection: Pipes
Another form of redirection occurs if the output from one command is directly
sent to another command. This is called a "pipe(line)" and represented by "|".
The following two lines
cat file1 file2 > sample_file
wc < sample_file
can be combined into one line by using a pipe.
cat file1 file2 | wc
This eliminates the need for temporary files.
- The command "grep" is used for searching through file content (eg.
grep 'wc' file). The command "history" lists the last
commands that were executed. Can you figure out how to combine
"history" and "grep" so that all commands containing "wc"
that were recently executed are printed? If you cannot figure
it out right away, start by redirecting the output from history
to a temporary file. Then combine "grep" and "history" into one
2.1 Wildcards and other special characters
The following characters have special meanings
- * means "any character including no characters"
- ? means "any single character"
- ; end of a command
- \ escape character
Execute the following commands:
(Note: "touch" creates empty files for given filenames.)
touch hello.txt progr.out newfile
ls *.txt; ls *.out
- Touch a file called "Hello World".
- List all files which have a three character extension.
- Create a directory "temp". Copy all files which contain a "t" in their name
from your home directory into "temp".
- Remove all files from your directory "temp". Use the option which asks for
confirmation for each file before deleting it.
3.1 Unix file permission
Unix file permissions are visible in a long listing ('ls -l').
Three levels of permissions:
- r - read permission
- w - write permission
- x - execute permission
Three levels of ownership:
- u - user
- g - group
- o - others
Example: "-rwxr-x---" means:
- owner: rwx
- group: r-x
- other: ---
Permissions are changed with "chmod WHO+PERMISSION FILENAME".
chmod o+r newfile
chmod g-x newfile
Permissions can also be changed using a numerical representation, which
is calculated by interpreting the permissions string as three binary
numbers. For example, "-rwxr-x---" corresponds to 111 101 000, which
corresponds to 1*4 + 1*2 + 1*1, 1*4 + 0*2 + 1*1, 0*4 + 0*2 + 0*1,
which corresponds to 750. Thus "chmod 750 FILENAME" changes the
permissions to "-rwxr-x---".
- List one of your directories. Then remove your own read and execute (search)
permissions from the directory. Try to list the directory again. Restore the
read access and try again. Finally restore the execute access.
- Remove the write permission from a file in your home directory, then try to
add the current month to the end of the file. Restore the write permission
and try again.
- Ask your neighbour for his/her username. List his/her home directory
and find out which files (if any) you have read access to. Change one
of your files, so that your neighbour can write to it. Test whether that
works. Can your neighbour delete any of your files?
- Check the permissions for all your files and directories according to
the following criteria: If you plan
on storing any coursework (or back-ups of your coursework) on your
Unix drive, set the permissions for those directories, so that no one
else can read them. Keep in mind that if you have a public_html directory, this
needs to be readable and executable by others. Your top-level directory
needs to be executable by group and others if you want to store
web pages in your public_html directory. But the top-level directory
does not need to be readable by group or others.
4.1 Symbolic links
Directories or files can be links to other directories or files. This is
similar to using pointers or references in programming languages. There
are many reasons for using links, such as, abbreviating long pathnames and
providing duplicate names for files to facilitate compatibility among
different flavours of Unix.
There are two different types of links: hard links and soft (or symbolic) links.
Hard links point directly to a file and may not span different file systems
or link to directories.
Soft links contain only an object's path name (a 'pointer') to the place where
the actual data is stored. They can span file systems, and may refer to directories.
The following example for a hard link assumes that you have a directory called "temp":
cal > hardfile
ln hardfile temp/hardfile2
ls -l temp/hardfile2
- Change the content of "hardfile" and check what this does to hardfile2.
cal > softfile
ln -s softfile temp/softfile2
ls -l temp/softfile2
- Notice the difference in the listing between hard and soft links.
- Remove both hardfile and softfile and see what that does to the linked files.
Unix provides several commands for monitoring and administering
processes and jobs. Below is a summary:
| ps ||
ps (all processes of terminal)
ps has lots of options but they depend on the flavour of Unix
| top || shows top processes in the system
| jobs || shows background jobs
| bg ||
puts specified job into background
| fg ||
puts specified job into foreground
| kill ||
kill [-signo] pid1 ...
sends signal to process
default for signo: -15 (usually terminates process)
-9 (always terminates process)
| sleep ||
example: (sleep 5; echo "Hello")&
| at ||
at time (scheduling processes at a certain time)
example: at 4:30am tomorrow RETURN bigjob RETURN ^D
| nice ||
nice command &
(executes command at a lower priority, useful for batch jobs)
| wait ||
(waits for termination of child process)
| nohup ||
nohup bigjob &
job keeps running after logout
- Try different ps options. Type "more &" then "nano &"
then "ps -l". Which of the
processes is the parent? What is the id of the parent process?
In what states are the processes?
- Type "jobs". Then bring the "more" process into the foreground.
Find the process id of the "nano" process and kill it.
- Type "emacs". Then put the process into the background using a
second terminal window. Then kill the emacs process.
- Find out what the current time is using "date". Using "at" print
the current month to a new file exactly 2 minutes later.
Unix commands are interpreted by a shell. Every terminal window has
at least one shell running in it. There are different types of shells:
sh, ksh, csh, tcsh and so on. You can find out what type of shell
you are using by
Overview of shells:
| shell || name || user start-up files || system-wide files
| sh || Bourne shell or Posix shell || .profile || /etc/profile
| ksh || Korn shell || .profile || /etc/profile
| csh || C shell || .login, .cshrc || /etc/csh.login
| tcsh || T C shell || .login, .tcshrc, .cshrc
| bash || bash: GNU Bourne Again SHell || .bash_profile,
.bash_login, .profile ||/etc/profile
A shell can be started by typing its name (eg "csh"). The command "printenv"
shows all environment variables which are currently set. Shells can be
customised according to user preferences and system-wide preferences.
The system preferences are in the /etc directory and usually executed
before the user preferences. Some shells (csh and tcsh) have separate
files for when a user logs in (.login) and when a user starts a shell (.cshrc).
The alias command can be used to create a name for a command or command
sequence. For example, "alias rm 'rm -i'" makes sure that rm is always
used with the -i option. Such an alias can be stored in the start-up file.
- Find out what shell you are currently using. Edit the start-up file
for your shell (or create a start-up file if you don't already have one).
Edit the file so that it prints the current date and time when you start the
shell. IMPORTANT: when editing a start-up file, you need
to make sure that it doesn't contain any errors before you logout.
After editing the file, start a new shell (by typing the name of the shell)
and test whether it works. If everything is ok, you can exit the shell
with "exit". It is a good idea to copy the original file to a backup
version (eg .cshrc_backup) before you make changes. If you mess up
your start-up files you MAY NOT BE ABLE TO LOGIN TO YOUR ACCOUNT again.
If that ever happens, there is still something you can do.
Find out what that is. (No, asking C&IT is not the right answer
in this case.)
- Check whether there is a command called "e". If there isn't,
add an alias to your start-up file which abbreviates your favourite editor
7.1 Some more advanced exercises
- Write an alias which stores your current working directory in a
file "~/.curdir" every time you use the cd command. Make sure only
one directory is ever present in the .curdir file.
- Have a message "Welcome, USER!" printed to the terminal exactly
1 minute after a new shell is started (replace USER with the username).
- Write a command that deletes all letters from a file and then sorts it
- Copy some web-page to a file. Using your favourite editor (or any
Unix commands you know), change the file into a text file, ie.
replace all <p>
tags by blank lines;
delete all HTML tags from the text;
insert line breaks so that lines don't wrap across a line;
if the file contains PC line breaks, delete these;
delete unnecessary blank space.
Try to achieve this as efficiently as possible,
using only a few commands.