UNIX

UNIX in 15 minutes

What is UNIX?  ---- UNIX is the name of the operating system of all workstations that you will use during the JANUS-practicum.

Why should you care? --- UNIX is a very powerfull, and convenient operating system, IF you know how to use it. But you will hate it, if you don't. So, if you never used UNIX before it might be a good idea to read the following short introduction to UNIX.

The following introduction is by no means complete. You can find more information under

http://www.idiom.com/UNIXhelp1.3/Pages


Changing your password

You already changend your password? OK. If not you should do it right now!

Just type

passwd

You will be asked to enter your old password and your new password (twice).

Old Password:  [enter your current password]
New password: [enter your new password]
Retype new password: [re-enter your new password]
Choose a password preferably between 6 and 8 characters in length, containing at least one non-alphanumeric character as ! or @ or % or ...


Important commands

Now that your account is safe let's continue with some of the most frequently used commands:


Pathnames:

If you use simple filenames as arguments of a command it will operate on your current working directory (you get a complete path of this directory by using 'pwd'). If you want to manipulate objects in other directories than you have to describe the path how to get to this directory.

In a path-description the names of directories are separated by '/'. For example "dir1/name" is the file called 'name' in the subdirectory 'dir1' of your current working directory. The directory '..' is a pointer to the parent directory of your current directory, '.' (a single period) is a pointer on the working directory itself. You can use any levels of indirection you want. So for example the path "../../dir1/" leads to the subdirectory 'dir1' of the parent of the parent of your current directory.

By now we described the way to a second directory starting from your current working directory (relative path). As well we can start at the root of the directory-hierarchy by preceding the pathname with a '/' (absolute path). 'pwd' will give you an absolute path to your home directory.

All possible path-components:

    .

the current working directory

    ..

the parent of the current directory

    /

at the beginning of a path-name: the root of the directory-hierarchy

in the middle of a pathname used to concatenate directory-names

    name

the directory called 'name'

    ~

your home-directory

    ~name

the home-directory of account 'name'

E.g.:

  • 'ls ..' displays the contents of the parent of your current working directory
  • 'cp ~islpra1/solutions/example .' copies the file 'example' from the directory 'solutions' of the account 'islpra1' to your current working directory
  • Wildcards:

    Now you can move files, copy them, remove them again and so on. But consider you don't have a single file, but lots of them. E.g.: you have 100 files called 'ex1', ..., 'ex100' and you want to remove them. Either you can use the 'rm'-command 100 times, or you use wildcards.

    Useful wildcards are:

      *

    matches any sequence of characters

      ?

    matches any single character

      [...]

    matches any character in the enclosed list or range

    Some examples:

    • '*' matches any sequence of characters. So 'rm *' will remove ALL files in the current working directory.Be Careful!
    • 'rm ex*' deletes all files with filenames that start with 'ex'.
    • 'rm ex?' would in the above example delete the files 'ex1',...'ex9' but the other files would be save, since the '?' can match one and only one character.
    • 'ls ex[127-9]' lists the files 'ex1','ex2','ex7','ex8',ex9'
    • 'ls ?x*0' lists all filenames where the second character of the name is a 'x' and the name ends with '0'
    • ...


    Some other useful commands

    • man commandname
    • !!! this one is useful !!! displays a manual page for the command 'commandname'.
      Try 'man man'.
    • less name
    • displays the contents of the file 'name' . You can browse the file by typing 'SPACE' (page forward), 'u' (page up). 'q' exits less. 'h' gives you a list of all key-commands
    • emacs name
    • edit file 'name'
    • grep pattern files
    • search a file/ a list of files for a string or regular expression
    • gzip name
    • No matter how much disk space you have, you never have enough!
      So please, if you have lots of data that you don't need so often, compress it.
      'gzip' compresses the file 'name', writes a new file 'name.gz' and removes the original.
    • gunzip name
    • Uncompress a gzip-file.

    Job control

    If you start a program it usually runs in the 'foreground' of your shell. This means that you have to wait until the program is finished before you can type any other commands in this specific shell. If you want to continue working in this shell without waiting for a program to stop you can also start it in the background by adding an '&' to the end of a command (e.g. 'emacs example &' starts an editor in the background and allows you to continue your work in the same shell).

    If a program is running in the foreground of a shell you can stop it by typing 'CTRL-c'. This works in most cases. If it doesn't use 'kill'. You can suspend a running foreground-jop by typing 'CTRL-z'. This job will then sleep until you wake it up by using 'fg' or 'bg'.

    If you log yourself out all processes that run in the foreground of a shell are terminated. Background-jobs wil continue running till they are done or get killed.

    • fg
    • continue a suspended program in the foreground of a shell
    • bg
    • continue a suspended job in the background
    • ps
    • shows a list of all the jobs (programs) you are running and their current status
    • kill pid
    • if for some reason you want to stop one of your programs you can kill it with 'kill' (if CTRL-c doesn't work).
      You can get the process-id (pid) by using 'ps'.


    Input/Output redirection

    UNIX considers any device attached to the system to be a file. And that includes your terminal!

    By default a program takes it input from your terminal (the keyboard) and writes it output back to your terminal (the shell in which you started the program). You can redirect both of it.

    An example: you would like to have a list of all files in your working directory. If you use 'ls' you will get this list, but only written on your screen. In order to collect the output a file you have to redirect the standard-output. Try 'ls > test'. This command will create a file 'test' with the contents of your current working directory.

    UNIX allows you to link two or more commands sing a pipe. The pipe takes the standard output from one command and uses it as the standard input to another command.

    command1 | command2 | command3

    The | (vertical bar) character is used to represent the pipeline connecting the commands. With some practise you can use pipes to create complex commands by combining several simpler commands.

    An example: if you now want to know how many files are in your working directory, you can use 'ls' to get a list of the files, take the output and feed it in a program that counts the lines of the output.

    ls | wc -l

    (use 'man wc' to find out mor about 'wc')

      < name

    redirect standard-input (take input from file 'name')

      > name

    redirect standard-output to file 'name'

      >> name

    redirect standard-output, append to file 'name'

      cmd1 | cmd2

    pipe output of 'cmd1' to standard-input of 'cmd2'

      >& name

    redirect error messages to file 'name'

    Original version from Detlef Koll (Thanks!)
    Last modified: Thu Feb 15 13:32:56 EST 2001
    Maintainer: tanja@cs.cmu.edu.