User Tools

Site Tools


unix_survival

This is an old revision of the document!


UNIX Command Reference

The command prompt in UNIX is called the shell, which typically uses a dollar sign ($) as its prompt. Here is a list of common UNIX commands which will work across many different versions of UNIX with discrepancies in Example/Notes.

CommandDescriptionExamplesNotes
asASsembleras input.s
ccC Compilercc input.cCreates an executable named a.out by default
cdChange working Directorycd /usr/local/gameson Version 6 this command is chdir
dfDisk FreedfDisplays summary of free disk space. May take time to complete on some systems
edEDitored filenameED is the standard (line) text editor
echoEchoecho ${HOME}Prints the value assigned to the variable HOME
envENVironment settingsenvDisplays the shell environment settings (see set))
ldLink Loaderld inputfile
lsLiSt directoryls -lprovides a long LiSting
manMANualman lsOn some systems, displays online manual pages. Try “man man” to learn more about “man.”
psProcess Statusps -a or ps -lprints All or Long Process Status
pwdPresent Working DirectorypwdPrints the working directory
setSET or display shell parameterssetSets or displays the shell environment settings (see env)
viVIsual editorvi filenamevi is a screen editor

On some systems typing games will print a list of games.

directorydescription
/This is the top level or root directory
/bincommon user programs are here (type echo ${PATH} to see more
/etcThis is the system directory
/libSystem library files
/usr“Unix System Resources” or “USeR” directory, depending on who you ask - contains additional binaries, libraries, manpages
/usr/games or /usr/local/gamesUsually contains the standard UNIX games

Useful UNIX References

The 4.2BSD Manual is a good guide to BSD systems (generally applicable to 4.3, 2.11 and V8 Research UNIX).

UNIX System V User's Guide (Second Edition) and UNIX System V User's Reference are a useful references for System V-based systems (or close relatives).

The UNIX Programmer's Manual (7th Edition) is an excellent overview for using UNIX for software development.

man.cat-v.org provides HTML-ized versions of man pages for many historical UNIX systems.

Old Unix XRef gives a very nice cross-indexed look at the 2.11BSD UNIX source code, if you're interested in seeing how the system is constructed from the kernel on up, or if you're interested in hacking the kernel source yourself.

Using the C Compiler

This wiki cannot provide an extensive tutorial for the C language, but we'll provide an example or two to get you started. The C Programming Language is the classic reference for this (and belongs on every UNIX hacker's bookshelf) and should apply to all of the UNIX systems we have online (although some may also support the later ANSI C specification(s) as well.

Use the editor of your choice to create the source file. We'll use ed here, as it's common to all UNIX systems, but vi may be a more friendly choice. This program is a version of your average “Hello, world!” program. It uses printf to output the string “Hello, world! to stdout and then exits. This example uses K&R syntax:

$ ed hello.c
?hello.c
a
int main(argc, argv)
   int argc;
   char** argv;
{
   printf("Hello, world!\n");
   return 0;
}
.
w
98
q
$

You should now have a file on disk named “hello.c”:

$ ls -l hello.c
-rw-rw-rw-   1 user       user             98 Apr  9 02:36 hello.c
$

This new C source file can be compiled into an executable using cc. The simple syntax for this is:

$ cc hello.c
$

This produces an output file named a.out. You can run this directly:

$ ./a.out
Hello, world!
$

Optionally, you can use the ”-o“ compiler flag to specify the output filename:

$ cc -o hello hello.c
$

Which will produce an output file named hello.

$ ./hello
Hello, world!

Here's a more complex program. It makes use of for loops to print a small multiplication table.

$ ed table.c
?table.c
a
int main(argc, argv)
    int argc;
    char** argv;
{
    int i,j;
    
    /* Use nested 'for' loops to print a 4x4 multiplication table. */
    for(i=1;i<5;i++)
    {
        for(j=1;j<5;j++)
        {
            /* '%d' means 'Substitute a numerical value here' */
            printf("%d x %d = %d\t", i, j, i * j);
        }
        
        /* '\n' is the "newline" character -- this moves the cursor to the next line
           for the next row of the table */
        printf("\n");
    }

    return 0;   
}
.
w
516
q
$

Compiling this as in the above example and running it yields:

$ cc table.c
$ ./a.out
1 x 1 = 1       1 x 2 = 2       1 x 3 = 3       1 x 4 = 4
2 x 1 = 2       2 x 2 = 4       2 x 3 = 6       2 x 4 = 8
3 x 1 = 3       3 x 2 = 6       3 x 3 = 9       3 x 4 = 12
4 x 1 = 4       4 x 2 = 8       4 x 3 = 12      4 x 4 = 16
$

Shell Programming

The shell itself is programmable via /shell scripts/. As with C, a complete tutorial is beyond the scope of this page but we'll provide a few examples to get you started. The below examples are for /bin/sh, the Bourne shell, as this is available on all online UNIX systems. Other common shells are ksh, csh, tcsh, and bash.

As with the C examples above, we'll use ed to create our source files. (Short shell scripting can also be done interactively at the command line.)

$ ed hello.sh
#!/bin/sh

echo "Hello, world!"
.
w
33
q
$

The first line (#!/bin/sh) is sometimes referred to as the “shebang” line (because UNIX programmers are weird) and tells the operating system what shell to invoke to execute the script file. Now we've created the script, and there it is on disk: <code> $ ls -l hello.sh -rw-rw-rw- 1 user user 33 Apr 9 05:56 hello.sh </code> But if we try to run it, we get: <code> $ ./hello.sh sh: ./hello.sh: Execute permission denied. </code> (The exact message may vary depending on what UNIX you're on.) This is telling us that the script we created doesn't have the execute bit set (only the read and write bits indicated by rw) – so it cannot be run. The chmod command can be used to set the execute bit on our new shell script: <code> $ chmod u+x hello.sh $ ls -l hello.sh -rwxrw-rw- 1 root sys 33 Apr 9 05:56 hello.sh $ </code> As you can see the hello.sh file now has the execute bit set (see the “x” in the file listing above). Now it can be executed: <code> $ ./hello.sh Hello, world! $ </code>

unix_survival.1586387364.txt.gz · Last modified: 2020/04/08 23:09 by joshd