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).

      cal
      cal 3 1991
      cal > file1
      more file1
      cal 3 1991 >> file1
      more file1

A command can also read input from a file, using "<". The following two lines are equivalent:

      more file1
      more < file1


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.


2.1 Wildcards and other special characters

The following characters have special meanings

Execute the following commands: (Note: "touch" creates empty files for given filenames.)

      touch hello.txt progr.out newfile
      ls *
      ls n*
      ls *.*
      ls h?ll?.txt
      ls *.txt; ls *.out
      touch Hello\!

Exercises:


3.1 Unix file permission

Unix file permissions are visible in a long listing ('ls -l').

Three levels of permissions:

Three levels of ownership:

Example: "-rwxr-x---" means:

Permissions are changed with "chmod WHO+PERMISSION FILENAME".

Examples:

      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---".

Exercises


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
      ls -l
      ln hardfile temp/hardfile2
      ls -l temp/hardfile2

      cal > softfile
      ls -l
      ln -s softfile temp/softfile2
      ls -l temp/softfile2


5.1 Processes

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
  • bg [[job]]
  • puts specified job into background
  • fg
  • fg [[job]]
  • 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
  • wait [pid]
  • (waits for termination of child process)
  • nohup
  • nohup bigjob &
  • job keeps running after logout
  • Exercises:


    6.1 Shells

    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

          printenv SHELL

    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 /etc/csh.login, /etc/tcsh.login
    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.


    7.1 Some more advanced exercises