UNIX Boot Camp
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.
- name: Every command has a name. Each command is actually a C executable.
- options: These modify the basic action of the command. They are always ....
optional. There are two kinds:
-l
(short format, single character) and verbose (--some-words). One command can have several options. - argument(s) These are things the command is acting on.
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.
Name | Options | Argument(s) | Action |
---|---|---|---|
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 | ||
-a | display hidden files, too | ||
cp | donor file, recipient file | copies 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. | |
-i | asks prior to clobbering any recipient file | ||
-R | donor must be a directory, duplicates a directory's file subtree. Use with caution. |
Now let's look at a few examples
ls
lists files in your cwdls -l fileName
lists filefileName
in long formatls -al dirName
lists contents of directorydirName
in long format, including hidden filescp
This command always requires at least two arguments, a donor and a recipient. If the recipient is a directory, you can have several files as arguments. All of these to it are copied into the recipient. If the recipient does not exist as a regular file, it is created and a copy of the donor file is placed into it. If the recipient does exist, its contents are replaced with those of the donor and are lost.
RTFM, Maggot!!! There are ways to get help with UNIX commands. We show three.
- The
man
: Typeman
then a command at the command prompt. This will bring up a manual page. You can navigate with the arrow keys, hjkl, control-d and control-u. Quit by typingq
. Vi search commands work in this setting. info
: Typeinfo
then your command to get information on the command.- The
--help
option: This gets you information when used on a UNIX command
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
You will need these commands.
mkdir
cd
touch
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.
rmdir
rm
Here is your second exercise. Create the file tree shown above, but you are not allowed to leave your home directory.
Important Commands
pwd
This tells your current working directory (cwd
).ls
This lists the contents of yourcwd
by default.cd
This is the change directory command. If there is no argument, you go to your home directory. Otherwise, you go to the directory you specify as an argument.ln -s
Use this to create a symbolic link. This can save you from maintaining duplicate files, which is always pocket bread.
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
touch
You can pass one or more file names as arguments. If the file does not exist, it is created. If it does, its last-modified date is moved to the present. This is often used to foolmake
into compiling all modules.mkdir
You can pass one or more names to this and it makes a directory by each name.cp
This copies files. It requires at least two arguments. If the last argument is a directory, items preceding it are copied into it. If the second arguent is a file, it is overwritten. If the second argument is a name for a file that does not exist, it is created.rm
This delinks a file from the file system. It is not reversible without great expense.mv
This requires at last two arguments. If the first argument is a regular file and the second is a file name for a file that does not exist, the file in the first argument is renamed to the second argument. If the second argument is a file that exists, it is overwritten by the contents of the first file. If the second argument is a directory, the item named in the first argument is moved inside of that directoryrmdir
This will remove one or more empty 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.
:wq
to save changes and quit.:q!
to quit without saving changes.:q
to quit if you haven't made any changes.:w filename
to save a copy to the indicated file.:w >> filename
to save a copy, appending it to the indicated file.
The Modes of vi | |||
---|---|---|---|
Mode | To Get In | To Get Out | Capabilities |
Command | default mode | exit vim | search, replace, mobility, read in files, write to file, cut, paste, copy |
insert | i, a, o, I, A, O and others | ESC | type text |
replace | R | ESC | type text over existing text |
visual | v, V or control-V | ESC | select 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.
u
user permissionsg
group permissionso
permissions for others
There are four possible permissions for files.
r
is read permission with octal value 4.w
is write permission with octal value 2.x
is execute permission with octal value 1.-
is no permission with octal value 0.
There are two ways to change file permissions. One is as follows
unix> chmod (u|g|o)(+|-)(r|w|x) filename
chmod ug-x file.foo
takes executable permissions from the user and the group.chmod g+r file.foo
gives others read permission.chmod go-rwx
takes all permissons from the group and others.
The other is the octal method.
unix> chmod abc filename
where
a
,
b
, and
c
are octal digits.
chmod 644 file.foo
gives read/write (4 + 2) to the user and read(4) to everyone elsechmod 600 file.foo
gives the user read/write permissions and no permissions to others.chmod 755 file.foo
gives rwx (4 + 2 + 1) permissons to the user and rw (4 + 1) permissions to everyone else.
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">
<head>
<title>Bare Bones Web Page</title>
<meta charset="utf-8"/>
</head>
<body>
<h2>Bare Bones Web Page</h2>
</body>
</html>
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.
Aliases
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.
#!/bin/bash
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.