11 January 2021

UNIX Boot Camp

picture of drill seargant

Maggot!!! You are a GUI-loving wimp! This will change! No more mouse for you! No more clicking, just the keyboard! Starting today!

When you Log in You are running a program on the server called the shell. It is running throughout your session. It waits for your to type in a command, passes it the kernel when you hit ENTER, and then passes along the kernel's reply. Think of it as a telephone connected to the OS.

Every running program on a UNIX machine is called a process. Every process is issued a PID, which is a unique identifier for that process. Every process has a cwd, which is the directory it can see directly into. "Your" cwd is your shell's cwd.

The three parts of a UNIX command Every UNIX command is a process; a lot of them run in a fraction of a second. Only the name is always mandatory to run a process. Sometimes other data is required.

Also, a UNIX command can accept input from the file stdin, or standard input, which, by default is the keyboard. We will discuss this fully when we come to UNIX filters.

Here are three examples of UNIX commands.

cd returns you to your home directory
a directory puts you in that directory
ls a directory list files in the specified directory
a file display the file's name
-l display in long format
-adisplay hidden files, too
cpdonor file, recipient filecopies donor file's contents to recipient. If recipient exists and is a regular file, it is overwritten. If the recipient does not exist, it is created. If the recipient is a directory a copy of the file is deposited in it.
-iasks prior to clobbering any recipient file
-Rdonor must be a directory, duplicates a directory's file subtree. Use with caution.

Now let's look at a few examples

RTFM, Maggot!!! There are ways to get help with UNIX commands. We show three.

Paths and Navigating the File System

Relative and Absolute Paths A relative path is a path that is relative to your cwd. An absolute path is a path that points to an absolute location in the file system.

What are . and ..?

Here is your first exercise. Write a sequence of UNIX commands that creates this file hierarchy. Circled items are directories, items in rectangles are regular files

picture of a file tree

You will need these commands.

Use the manuals to learn what you need. Having done this, now write a sequence of commmands to remove what you just built. These commands will come in handy.

Here is your second exercise. Create the file tree shown above, but you are not allowed to leave your home directory.

Important Commands

The Environment Variable $PATH and friends $PATH tells the system where to look when you enter a command.

echo can be used to print an environment variable; printenv will print all of your environment variables.

Managing Files and Directories

Editing with vim

To enter vim, type vi filename at the command prompt. Extensions trigger syntax coloring for a zillion languages. You need to be come very good at this, so practice it. The server has vimtutor. Check it out. You will find numerous vim commands that are useful in sed. The vim editor is a highly efficient way to get your thoughts turned into bytes.

To exit, be in command mode and hit one of these.

The Modes of vi
ModeTo Get InTo Get OutCapabilities
Commanddefault modeexit vimsearch, replace, mobility, read in files, write to file, cut, paste, copy
inserti, a, o, I, A, O and othersESCtype text
replaceRESCtype text over existing text
visualv, V or control-VESCselect text

You should view the videos on my YouTube channel on vim. You can open a terminal window and follow along, stopping it when you want to do a little experimenting.

Ownership and Permission

Using chmod Ownership of files is baked into UNIX system. There are three rings of permission.

There are four possible permissions for files.

There are two ways to change file permissions. One is as follows

unix> chmod (u|g|o)(+|-)(r|w|x) filename

The other is the octal method.

unix> chmod abc filename

where a, b, and c are octal digits.

For a directory to be seen through, it must have execute permissions. If you maintan a website, your home directory should have 711 perimssions and your public_html directory should have 755 permissions. Files you want visible in this directory should be marked with 644 permissions.

Simple Exercise Download the file skeleton.html on the left. Here are its contents.

<!doctype html>
<html lang="en">
    <title>Bare Bones Web Page</title>
    <meta charset="utf-8"/>
    <h2>Bare Bones Web Page</h2>

Transfer this file to the server. When you get it there, rename it index.html. Set the permssions so it is visible. Now open your browser to this URL: http://cs.ncssm.edu/~yourUserNamea and your page should be visible.

Now make a copy of index.html to forbidden.html. Set the permissions to this new file so it can't be seen.

Shell Scripts and Cusomization

One huge advantage of the command-line interface is that it is programmable, as well as being customizable. The complete manual for BASH can be found here.

Comments in BASH A one-line comment is a space then a pound sign(#). Do not forget the space or you will get an ugly surprise. That means: deliberately make that mistake and see what happens.


Writing a Shell Script A shell script is just a bunch of UNIX commands in a file. BASH is a turing-complete programming language. Using a .sh extension gives you proper syntax coloring.

The first line should be the shebang line which looks like this.


Exercise Make a shell script that builds the file tree show above, and a second shell script that demolishes it.

Making a Python Program Executable Here is the whole shebang.

#!/usr/bin/env python3

Exercise Create a "Hello world" python program. Make it executable and put it in ~/bin Then try running it from anywhere in your file system.