User Introduction to Unix/Linux

Login

Now what?

Once you have logged on, you will see your desktop. It will be more or less as you left it, no matter which of the Linux systems you login from. You will want to open a Console, or a UNIX Shell (known also as an xterm, or X-terminal) in order to enter commands to the command-line interface, or the shell.

Setting your password

The vi text editor

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).

Questions about vi may appear on tests and exams.

Finding information about Linux

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

Basics of the Bourne-Again-Shell (bash)

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.

Getting started

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>

History Mechanism (like doskey in DOS)

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
(where n is an id number from a prompt)

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"

A few commands to start with

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

General notes

Some useful UNIX commands to get started

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

Fifty or so general Linux commands (and other things)

The commands listed below are ones that you will use throughout the term. You must become familiar with each command and its main options.

Informational commands

man

ls (and options!)

set

du

echo

df

who

ps

whoami

jobs

date

find

File editing commands

vi

sed

tr

cut

File and Directory manipulation commands

cp

mv

rm

cd

mkdir

pwd

rmdir

chmod

ln

ln -s

popd

pushd

File and Filter commands

cat

grep

head

tail

more

wc

touch

test

sort

echo

cut

tr

Input/Output (re)direction (and things)

<

script

>

printf

>>

lpr

2>

|

<< XXXX

&>

Miscellaneous commands (and things)

^Z (control-Z)

kill

^C (control-C)

help

^D (control-D)

exit


Editing text files

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:

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.

VI DIAGRAM

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.

Adding new text (from command mode into insert mode)

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


Moving around without the arrow keys (within command mode)

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

Editing text (from command mode)

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

Search and Replace (from ex mode)

/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)

Save and Exit (from ex mode; you can also use ZZ in command mode)

: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).

Program Debugging

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.

Source-level bash code debugging

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



Source-level tcsh code debugging

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.