User Introduction to Unix/Linux
Your user id is your standard College account, 4 letters from your last name and 4 digits.
Your password is the string "just2day" (without the quotes, of course!). Change it at once with the yppasswd command (see below).
Your HOME directory, /home/<userid> or ~ for short, will appear to contain only the directory Desktop.
Unix/Linux used to use the command passwd to change passwords, but since many systems, including ours, use the Network Information System (NIS) formerly known as the "Yellow Pages" for network-wide password control, you will use the command yppasswd.
Your password should contain both letters and digits, and even punctuation, and must be at least 6 characters long. Ideally, you will mix upper and lower case letters.
Knowledge and skill in using the vi editor is a required component of this course. Please read pages 71-72 in "Practical Linux" and pages 301-302 in "Linux Shells by Example" for some information about vi. There's more information in this document, and in the man page for vi (man vi or man vim).
In addition to your textbooks, you should learn how to use the man command to read the manual pages that are online on all Linux and Unix systems. Some systems will have additional online documentation, but all have man pages. Enter the command below to get started:
man man
Reference: Chapter 8 of "Linux Shells by Example"
In Unix and Linux, "shell" is the term used to describe the command-line interpreter (roughly equivalent to command.com in DOS). In this course, we will use the bash shell because of its wide popularity on Linux, although there are other shells available such as tcsh, sh, ash, and more.
Starting bash: |
System Prompt$ bash |
Checking your login or current shell setting: |
System Prompt$ echo $SHELL |
File name completion (to avoid typing long file names) |
System Prompt$ vi /etc/pas<TAB> |
Displaying the current history buffer |
System Prompt$ history | more |
Re-executing the last command |
System Prompt$ !! |
Re-executing any previous command |
use the up and down arrow keys |
Re-executing any previous command |
System
Prompt$ !n |
Editing a previous command |
use the up and down arrow keys to select the command, then use vi keystrokes to edit it. |
Alias mechanism (like macros using doskey) |
System Prompt$ alias ll="ls -l" |
List the files in the current directory |
System Prompt$ ls |
Create an empty file |
System Prompt$ touch some_file |
Create a small file |
System Prompt$ echo "some words" > another.file |
List the files in long format: |
System Prompt$ ls -l |
Create a new directory |
System Prompt$ mkdir some_dir |
List the files in a directory |
System Prompt$ ls -l some_dir |
List all the files in a directory |
System Prompt$ ls -la some_dir |
List just the directory named by itself |
System Prompt$ ls -ld some_dir |
Change to another directory |
System Prompt$ cd some_dir |
See what the current directory is |
System Prompt$ pwd |
Look at a short file (less than one screen) |
System Prompt$ cat short.file |
Look at a long file one screen at a time |
System Prompt$ more long.file |
Linux is case sensitive. For example, the file names Demo.file and demo.file are completely different files in Linux and can both reside in the same directory without conflict.
This
case sensitivity also applies to usernames, passwords, command
names, and so on. Take care with
CapsLock: the commands
cc and CC are
not the same.
This is also the likeliest common problem in
failing to login successfully.
In Linux, the concept of "drives" as in Windows does not apply. There is no C:, or even an A: mountable drive. The entire file system appears as one volume (even when it spans multiple hard disks or a network). Any directory (or folder) can be addressed by a pathname originating at the root directory "/", your HOME directory "~", or from the current working directory "." or its parent directory "..".
Linux is designed for multi-user, multi-tasking, network-based operation, so a user's HOME directory is typically more important to them than the system's root or other system directories.
Linux uses long file names (255 characters maximum), and the dot character has no special meaning in a file name. As a result, you can easily have filenames with many dots in them, such as demo.file.version.1.
Linux has no concept of a file extension (such as Microsoft Windows uses) built into the operating system. Some applications will use extensions such as .c, .o, .tar, and .gz, mostly for your convenience. Executable files, including programs, do not normally end with a .exe extension.
The
forward slash / is
used to separate directory names in a path instead of the backslash
character \ that
is used by all the Microsoft operating systems including Windows.
Command options are normally placed immediately after the command name. Each option is prefixed with a dash - character, and options can often be either combined or separated (that is, xxx -a -b and xxx -ab or even xxx -ba are usually equivalent ways to invoke some imaginary command xxx with options a and b).
Command |
Meaning |
ls |
List the names of the files in the current directory |
ls -l |
List the files in long format, with additional detail |
mkdir demo1 |
Make (or create) a new directory named demo1 |
rmdir demo2 |
Remove (or delete) the directory named demo2 |
cd demo3 |
Change the current working directory to demo3 |
pwd |
Print the name of the current working directory to the screen |
cd |
Change the directory to the user's own HOME directory |
cp A B |
Copies the file named A to the file B, creating a new file if needed |
mv C D |
Move a file or directory to a new location, or simply change its name |
rm E |
Remove (delete) the single file E |
rm * |
Remove every file in the current working directory (be careful) |
mv F G |
Rename the file or directory F by moving it to the new name G |
vi H |
Use the visual interactive editor (see below) to edit file H |
gcc -c src.c |
Compile the file src.c with the GNU C compiler without linking |
The commands listed below are ones that you will use throughout the term. You must become familiar with each command and its main options.
man |
ls (and options!) |
set |
du |
echo |
df |
who |
ps |
whoami |
jobs |
date |
find |
vi |
sed |
tr |
cut |
cp |
mv |
rm |
cd |
mkdir |
pwd |
rmdir |
chmod |
ln |
ln -s |
popd |
pushd |
cat |
grep |
head |
tail |
more |
wc |
touch |
test |
sort |
echo |
cut |
tr |
< |
script |
> |
printf |
>> |
lpr |
2> |
| |
<< XXXX |
&> |
^Z (control-Z) |
kill |
^C (control-C) |
help |
^D (control-D) |
exit |
Although there are many editors and word processors available for Linux, the vi visual editor is the standard editor that is available on every Unix and Linux system. You may find vi difficult to learn and use at first, but you will come to appreciate its many powerful features.
The version of
vi often found on Linux systems (including ours) is a
derivative named vim
(vi
improved)invoked by
typing either vi or
vim at the command prompt.
The key to using vi is to remember that it was developed before cursor controls like the arrow keys, Page Up/Down keys, and other editing keys were commonly available on keyboards. To work around this, all forms of vi define three basic operating modes:
text insertion (input mode; characters go in as text);
cursor movement and simple editing (command mode; characters are executed as commands);
sophisticated editing operations (line edit or ex mode using the bottom line of the screen).
Therefore, each key on the keyboard can have three entirely different meanings, depending in which mode the editor receives it. Remember: Upper-case and lower-case letters are different commands! Try both p and P for pasting, for example, in command mode.
When you first start vi, you are in command mode. Press i to enter text input mode and type your material. Return to command mode by using the ESC key to end insert mode. Make changes or corrections from input mode as needed. When you are finished, go to edit mode to save your file and exit. Use :w to write your file to disk, or :w <filename> if it needs a new name, and :q to exit. You may combine :w and :q as :wq for speed.
The diagram below illustrates methods of moving between each mode, and some of the keys available in each mode.
Some basic commands for vi
These basic commands are enough to get you started and to complete most simple editing tasks. The more commands you learn, the faster you will work.
i insert new text in front of cursor |
o open a new line after (below) cursor |
a insert new text after cursor |
O open a new line before (above) cursor |
A insert new text at the end of the line |
|
k move backwards (up) one line |
|
- move backwards (up) one line |
|
h move backwards one character |
l (lower case L) move forward one char |
b move backwards one word |
sp move forward (right) one character |
j move down (forward) one line |
|
¿ (ENTER) move forward one line |
|
^ move to the start of a line |
$ move to the end of a line |
x delete one character (under cursor) |
dd cut (delete) current line |
r replace character (under cursor) |
yy copy (yank) current line |
dw delete one word (containing cursor) |
p paste most recently cut/copied text |
cw change word (from cursor) |
u undo last editing command |
/text search forward for next "text" |
?text search backwards for previous "text" |
:s/old/new/ replace old with new once |
|
:s/old/new/g replace all occurrences of old with new on current line only |
:1,$s/old/new/g replace all occurrences of old with new on all lines from 1 to $ (end) |
:w filename write as filename |
:q quit, exit, leave vi |
:w write using existing filename |
:q! quit, discarding changes |
You will often find
vi commands in other places as well, either derived from
vi itself or often from a common ancestor program. For
example, the / and
? search commands work in
more and man,
and a close relative of :s
works in sed.
The use of h,
j, k,
l is guaranteed to move the cursor on all compatible
editors derived from vi,
but many implementations including
vim now also support the arrow keys on the PC keyboard
(both the "inverted T" and the keypad when
NumLock is off).
Your programs, both C-language and shell scripts, will not work perfectly the first time (a great surprise to you, I am certain). Many people use print statements of the appropriate flavour to debug their programs, a barbaric practice suitable only for the most trivial of programs.
Real programmers use professional debugging tools whenever possible.
The bash shell can display each line before or after all substitutions, or merely check the script syntax. Tracing can also be turned on and off inside a script:
Command |
Option |
What it does |
bash -x scriptname |
Echo option |
Display after substitutions |
bash -v scriptname |
Verbose option |
Display before substitutions |
bash -n scriptname |
Noexec option |
Check syntax |
set -x |
Turn on echo |
Start trace |
set +x |
Turn echo off |
Stop trace |
Including -x on the first line of the shell script will cause the csh program to provide a certain amount of diagnostic output at the source level, but no interactive debugging tool is provided. You will have to place input statements (to pause execution) and output statements (to display certain diagnostic values) in addition to the execution trace.
You can also temporarily add the debug option by entering:
tcsh -x name_of_script [list of arguments]
More sophisticated scripting tools, such as Perl, have more sophisticated debugging tools.