More UNIX Concepts

last updated 1/20/08

A significant philosophy in the design of the Unix operating system is the user environment. The goal was to build a set of tools that can easily be put together to get a variety of work done. Most utilities and simple programs written in Unix assume that the input will be from the standard input and the output to the standard output.

The standard input is often the keyboard, but can be a file or the output of another program. The writer of the program/utility need not know that. Unix will direct the input from any of the these sources into the program; the writer need only worry about the content of the input stream not its source.

The same is true for the output.  Unix will direct the output to the designated destination: screen, file or the input of another program.

 

 

 

 

 


Some miscellaneous commands  

more [filename] display the file one screen at a time; space to view next screen; b to backup a screen
head [-n] [filename] display the first n lines of the file
tail [-n] [filename] display the last n lines of the file
pwd show the current working directory
wc [filename] count the characters, words and lines in a file
sort [filename] sort the lines in the file

 
LAB exercise:  Experiment with these commands on the file /academic/cs2/naminghosts

 

 

 

 


File redirection

The standard input and/or standard output can be redirected at the point of running a command.  You don't need separate programs to handle file I/O versus keyboard input or screen output.

<file redirects the standard input to be the contents of the file

>file redirects the standard output to the file

>>file appends the standard output to the file

Consider the cat command and its interrelationship to file redirection.

cat  short for catenate (concatenate) - copies the standard input to the standard output
cat filename when a file is present it redirects the input from the file to the standard output 
(commands can analyze the command line options)
cat <filename  does the same thing (sometimes commands assume the file argument as input)
cat >filename copy the keyboard input to the file (quick file creation)
cat <f1 >f2 copies f1 to f2 (same as cp f1 f2)

 

LAB exercise:  Experiment with these commands on the /academic/cs2/naminghosts file.
Then, using the
cat command, create a file whose name is your last name.  Your full name should be on a line in the file.

 

 

 

 


Pipes

program1 | program2 "pipes" the standard output of program 1 to the standard input of program2 without the use of intermediate files.

ls -l | sort +3 print the directory listing by size (4th column of data in ls -l)
ls | wc how many files in the directory
program | enscript -2r -Pmainlasr print the output in enscripted form to printer mainlasr
program | tee file | more  capture the output of the program into the file and page it on the screen

 

LAB exercise:  Try the first two commands to test the pipe concept as directed by the instructor.

A "valuble" prize to the first person who can come up with the command to sort a full directory listing in descending order of size, ignoring leading spaces in the size of the file, and showing the output a page at a time on the screen.

 

 


Wildcards in filenames and paths

* matches 0 or more characters (anything)

? matches exactly one character

* and ? can be used in directory references as well

Examples

Assume the following directory structure

Directory ~ contains
Files:
demo.txt   first.java  typescript  .cshrc  .login  .forward  first.class
Subdirectories: cs2  personal

Directory cs2 contains
Files: hello.java
   hello.java  hello.class   typescript  second.java  second.class  aesop.txt
Subdirectories: proj2  proj3
Directory personal contains
Files:
  lists  resume.txt  grades.doc  courses.doc
Directory proj2 contains
Files:
Date.java TDDate.java IncDate.java Date.class TDDate.class IncDate.class
Directory proj3 contains
Files:
stringList.java  stringList.class  ssl.java ssl.class TDssl.java   TDssl.class
 

 

 


Searching  

grep is the typical Unix search command (Global Regular Expression Print)

grep string searches the input for lines containing string and print them to the standard output
ls -l | grep jan  list all the files dated january

  Some meta characters used in string searches and filename wildcards:

(period) match any single character
* (asterisk) match any string
^ (carat) beginning of line
$  end of line
[char list] or [^not char list] any character of list or range

 

Example:  What do these commands do?

grep ^i *.java
grep "^[A-z]" *.java

 

 


Directory References  

.  the current directory
..  up one directory level
~ the home directory
~user  the home directory of user
cd change directory
pushd change directory but remember last working directory
popd switch back the to the last working directory
mkdir dirname make directory
rmdir dirname remove directory

 

 

LAB exercise:  Make a subdirectory as directed by the instructor. Now pipe a full directory listing (lf -l) through enscript to the printer in the classroom.

 

 


Alias commands

alias symbol command

You may look at a sample login template file in the /academic/cs2 directory, called login.template

To copy it to your directory, you could use the command
cp /academic/cs2/login.template ./.login
and edit the file for your tastes.

 

 

 


Capturing execution runs for printing

When you turn in an assignment you need to print source listings, data file contents and demonstrate the execution of the program. Unix has an easy way to capture your interactive session to a file that you can print later.

script - this command will begin capturing your keyboard input and command/program output to the file typescript. You really start another shell session.

You then just type in the commands you need: cat the program, run the programs, g++, etc. When you are done you press ctrl-D to end the current shell session and return to the previous one.

DO NOT EDIT A FILE WHILE scriptING !!! Only script when you are ready to print a final run.

If you make a mistake the backspaces are part of the typescript file. You may want to edit the typescript file before you print it out.

lp -dc102 typescript
or
enscript -2r -Pc102 typescript - these will be the typical commands to output the screen capture.