A Brief Intro to Unix
The Unix command line interface (also used by Linux and MacOS) 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; the default for student accounts in our lab is bash. All of the common Unix shells support supersets of the basic command language described here.
In general, all Unix shells prompt for interactive input, and the prompt string can be set to just about any text. Here, we'll assume that the prompt looks like this before you begin typing:
Suppose executable is the name of a file holding an executable program; note that Unix variants do not require a suffix like .exe to indicate the file type. Given this, you'd run executable by typing a line that ends up looking like this (what you typed is shown in bold):
With this command, your program will run with input from the keyboard and output to the terminal 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 instead of the keyboard, you can do something similar, and if you want, both input and output can be redirected 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, and it's all on the web.
Unix (and Linux) file names are case sensitive. Capitalization matters, so the names FILE and file refer to two entirely different files.
Many parts of Unix give no special meaning to dots in file names, but many appications pay close attention to them. 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.
Unix maintains a tree-structured directory hierarchy. File names starting with slash / always search the tree from the root, while other names are interpreted from the current directory. So, /usr/bin is the global directory that holds many of the common executables, while bin is a file or directory in the current directoy.
The special file names . and .. stand for the current directory and its parent. The shell interprets the character * as a wildcard, so the filename prog.* will match prog.o and prog.c and even prog.html. The special file name ~ is your home directory, and if you're running a window manager, ~/Desktop is your desktop. All the files in that directory will appear as folders on the screen.
When the shell searches for an executable, it first tries the file name by itself; if that fails, it looks for it in a list of directories, the search path. This may include the current directory, but it also includes /bin and /usr/bin and several others.
Moving around in the file system cd dir change current 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 working 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 screenful 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