Unix Workshop

Note: please be careful with commands such as "rm" or commands that you not fully understand. Don't execute such commands in a directory that contains important files. The instructor will not be responsible for lost or damaged files.

In general it is a good idea to create a .trash directory and to alias rm as "mv !^ /home/user_name/.trash". To empty the .trash directory you need to reset the alias of rm to rm. Other simpler options are aliasing cp, mv, rm by their -i options and safety features in the login files, such as "set noclobber" in .cshrc.

1) Files, Processes and Users

Users: sessions and communication

logname
who [am I]
quota -v
write write username terminal
mesg
script
  • script filename
  • creates a logfile for the session, exit with "exit"
  • Exercise

    Login to the computer of your neighbor and write to him/her.

    Common tools

    man examples: man man; man -k calendar; man 5 man
    cal
    calendar (a reminder service)
    look look word (dictionary look-up)
    ispell a spell check (an elaborate version of "spell")
    date

    File structure

    . The current directory
    .. The next higher directory
    cd
  • cd
  • cd name
  • cd /name
  • cd ./name
  • cd ../name
  • pwd
    mkdir mkdir -p -m "a=r" this_level/next_level/last_level
    rmdir rmdir [-i] -p this_level/next_level/last_level
    cp
  • cp [-i] old_file new_file
  • cp [-i] file1 [file2 ...] dest_dir
  • cp -r [-i] dir_tree1 [dir_tree2 ...] dest_dir
  • mv
  • mv [-i] old_file new-file
  • mv [-i] file1 [file2 ...] dest_dir
  • mv [-i] dir_tree1 [dir_tree2 ...] dest_dir
  • rm
  • rm [-i] [-r] file ...
  • rm -i file1 file2
  • rm -i directory/*
  • ln
  • ln [-i] old_file new_file (hard link)
  • ln [-i] -s old_file new_file (symbolic link)
  • dircmp dircmp dir1 dir2

    Exercises

    Do the following things using as few commands as possible! (Write down the commands you use.) Create one directory called abe with a subdirectory homer that contains the files bart, lisa and maggie. Create another directory called marge that contains files that are called bart, lisa and maggie and that are linked to homer's files. Write a sentence into each of the three files (use "echo" to do this). Create another directory called simpson and move everything else there. Compare marge and homer to see whether their files are identical. Remove everything except an empty directory "marge".

    Displaying information about files

    ls
  • ls -a (show hidden files)
  • ls -l (long listing)
  • ls -ld (directory without contents)
  • ls -i (inode: shows hard links)
  • ls -R (recursive)
  • ls -ct (sort by last time modified)
  • ls -ut (sort by last time accessed)
  • ls -p (adds / after directories)
  • ls -F (adds /, *, @)
  • file (shows file type (sometimes))
    wc (counts lines words characters)
    which (finds programs according to paths and aliases)
    find
  • p 309
  • find pathname expression
  • find . -name "*.txt" -print
  • find . ! -name "*.???" -print
  • find . \( -name "*.??" -o -name "*.???" \) -print
  • find . -size +2 -print (more than 2 blocks)
  • find . -exec ls -ld {} \;
  • find . -name "*.bak" -exec rm {} \; (dangerous!)
  • find . -name "*.bak" -ok rm {} \; (safer)
  • find . -atime -n -print (accessed less than n days ago)
  • find . -mtime -n -print (modified less than n days ago)
  • find . -ctime -n -print (accessed or modified less than n days ago)
  • find $HOME -ctime -7 -type f -print
  • (shows regular files that were accessed or modified less than 7 days ago)
  • find / -user "stan" -print
  • find / -user 25 -print
  • find / -group "sys" -print
  • find / -type f - perm 0744 -print
  • Exercises

    a) Try all of the options for ls mentioned above. When were your last files accessed/changed? Execute "ls -lut" then "ls -R" and then one miute later "ls -lut" again: what do you notice?
    b) Which of your files in one directory contains the most characters?
    c) What is the full path name of "ls"?
    d) List all files in any subdirectory under your current directory that have an "html" extension using "ls". Do the same using "find".
    e) Do a long list of the current directory.
    f) Compare (find . -name "*d*" -print) and (ls *d*).

    a) Find all files in /etc that have a "d" extension.
    b) Find all files in the current directory that have an extension.
    c) Find all files in the current directory that don't have an extension.
    d) Find all files of block size smaller than 1 or larger than 3.
    e) Find all files of block size larger 2 and list them in a long listing.
    f) Find all files that were modified today.
    g) Find all directories on the system that are owned by you.
    h) Find all files on which everybody has write permit.

    Displaying file content

    cat
  • cat file1 file2 > file3
  • cat -n (prints line numbers)
  • cat -r (don't print multiple blank lines)
  • cat -v (prints control characters visibly)
  • more
    head head [-count] [file1 file2 ...]
    tail
  • tail +|-[count][lbc] file
  • tail -f file (used to check on a file being built)
  • Exercises

    Display the last 15 lines of a file. Display the first 5 lines of a file first using head, then using tail. Display the first 5 lines with line numbers.

    Comparing and manipulating file content

    Chapter 14

    sort
  • sort file1 file2 ... > new_file
  • sort file -o file
  • sort -r (reverse order)
  • sort -f (fold lowercase letters to uppercase)
  • sort -d (dictionary order)
  • sort -n (numeric order)
  • sort -b (leading blanks ignored)
  • sort -u (unique: suppress duplicates)
  • uniq
  • uniq input_file output_file (omits adjacent duplicate lines)
  • uniq -d (output one copy of each duplicate)
  • uniq -c (count how many duplicates found)
  • crypt
  • crypt encrypted (encryption)
  • crypt plain (decryption)
  • cmp
  • cmp file1 file2
  • (char and line where files first differ)
  • diff
  • diff file1 file2
  • (detailed list of differences)
  • split
  • split -n input_file output_file
  • (n denotes the number of lines)
  • tr
  • tr a A tr '[a-z]' '[A-Z]' < file
  • tr '[A-Z]' ' ' < file
  • Exercise

    Replace all blanks in a textfile by newline characters (newline characters can be represented as '\012'). Sort the output and then display it counting the duplicates. Sort it again so that the most frequent words are displayed first. Display only the first ten lines of that result.

    Command terminators

    newline (enter)
    ;
    &
  • child process runs in background
  • typing ^z in some applications has the same effect
  • Exercise

    Execute the following: a) date RETURN who RETURN
    b) date; who
    c) date & who

    Processes

    Chapter 9

    ps
  • ps (all processes of terminal)
  • ps -d (all processes except group leaders)
  • ps -e (all processes)
  • ps -u ulist (all processes of users in ulist)
  • ps -f (full listing)
  • ps -l (long listing)
  • 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
  • Exercise

    a) Try the different ps options. Type "more &" then "pico &" then "ps -l". Which of the processes is the parent? What is the id of the parent process? In what states are the processes?

    b) Type "jobs". Then bring the "more" process into the foreground. Find the process id of the "pico" process and kill it.

    c) Type "emacs". Then put the process into the background using a second terminal window. Then kill the emacs process.

    d) Find out what the current time is. Using "at" echo "hello" to a new file to be executed exactly 2 minutes later.

    XWindows

    Chapter 19: pages 504 - 508
    xterm
  • xterm options:
  • bg, fg, fn (background, foreground, font)
  • e (execute)
  • help
  • sb (scroll bar)
  • sl (lines in text buffer)
  • xhost
    xclock
    xcalendar
    xkill
    xman

    Exercise

    Open xterm window with black background and white foreground that runs in the background.