A Brief Intro to Unix

Part of the 22C:50 System Software support pages
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science


Introduction

The Unix command line interface is supported by command interpreters called shells. These are widely standardized, but the shell is not an integral part of Unix, so users have written several. The oldest is the Bourne shell, but there is the C shell, csh, the Bourne Again shell, bash, and many others; student accounts in our lab use tcsh.

All of the common Unix shells support supersets of the same basic command language that is described here.

Command format

Suppose executable is the name of a file holding an executable program, and suppose your shell prompts for input with the text prompt: Given this, you'd run executable by typing a line that ends up looking like this:

prompt: executable

With this command, your program will run with input from the keyboard and output to the current window. If you want to redirect the output of the program to a file named outfile, this works:

prompt: executable > outfile

If you want to run the program with input from a file named infile you can do something similar, and of course, both input and output can be redirected if you want, as follows:

prompt: executable > outfile < infile

Some programs take command line arguments or options. For example, the C compiler cc takes the name of a source file as an argument, so to compile the file called program.c you would type this:

prompt: cc program.c

By default, this will produce an executable called a.out; you can change this using the -o option. For example, if you want your output to go to program (with no .c extension) type:

prompt: cc -o program program.c

Many Unix programs accept the -help command line option, but sadly, many do not. Most Unix systems have on-line documentation, using the command man, so for example, if you want to get documentation for the ls command, you'd type:

prompt: man ls

The Unix programmer's reference manual is, historically, just the collection of the output from the man command for all of the commands, functions and other items in the on-line manual. You can buy various versions of this in most big college-area bookstores.

 

File Names and Directories

Many parts of Unix give no special meaning to dots in file names, but many appications pay close attention to these extensions. For the C compiler, for example, file names ending in .c are taken to be names of source files, while names ending in .o are taken to be names of object files. By convention, names ending in .sh are usually files of shell commands (shell scripts), and names ending in .h are header files for C programs, but this is just a convention.

Unix maintains a tree-structured directory hierarchy, and all references to file names are interpreted in terms of this hierarchy. File names starting with slash / always search the tree from the root, while file names that don't start with slash are usually interpreted from the current directory. So, /usr/bin is the directory that holds many of the common executables, and /usr/local/bin typically holds locally added executables.

The special file name . always stands for the current directory, and .. always stands for the parent directory of the current directory. The shell interprets the special character * as a wildcard, so the filename prog.* will match prog.o and prog.c and even prog.html.

When you type a program name in to the shell (but only there!), the shell doesn't only look in the current directory, it looks along a list of directories, the search path. Usually, this includes the current directory, but it also includes /bin and /usr/bin as well as several others.

Some Common Unix Shell Commands

Moving around in the file system
      cd dir  change directory to dir
      mkdir dir  make a new directory named dir
cd ..  change directory to the parent of the current one
pwd  print the name of the current directory
ls  list the contents of the current directory
ls dir  list the contents of the directory dir
 
Working with files
mv file1 file2  change the name of (move) file1 to file2
mv file dir  move file to the directory dir
mv file .  move file to the current directory
cat file1 > file2  make a copy of file1 called file2
rm file  delete (remove) file (use rmdir for directories)
rm *.o  delete all files with a .o extension
 
Dealing with text files
vi file  use the vi editor to edit file
emacs file  use the emacs editor to edit file
more file  output file to the screen, one page a at a time
cat file  blindly dump file to the screen, all at once
cat file1 file2 > file3   concatenate file1 and file2 into file3
 
Compiling programs
cc file.c  compile file using the default C compiler
gcc file.c  compile file using the Gnu C compiler
make file  use make plus Makefile to bring file up to date
 
Other
man command  output the manual page for commmand
man function  output the manual page for the C library function
grep "string" file  find and print lines containing string in file
grep "string" *.c  search for string in all files with .c extensions
sort file1 > file2  sort the lines of file1 into file2