Introduction to UNIX Documentation Introduction to UNIX Documentation, Release 1.0 important for the

  • View
    2

  • Download
    0

Embed Size (px)

Text of Introduction to UNIX Documentation Introduction to UNIX Documentation, Release 1.0 important for the

  • Introduction to UNIX Documentation Release 1.0

    Oliver Beckstein

    January 13, 2012

  • CONTENTS

    1 Unix Basics 3 1.1 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 Navigating file system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.3 Copy, renaming, deleting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.4 Looking at files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.5 Shell name generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.6 Input/Output Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.7 Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.8 Filter excercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.9 Access rights (permissions) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.10 Other useful commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.11 Unix variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.12 General Unix gotchas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.13 Setting up an editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

    2 vi/vim essentials 19 2.1 Moving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.2 Quitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3 Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.4 Editing text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.5 Undo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.6 Saving file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.7 More editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.8 More movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.9 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.10 Search and Replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.11 More on inserting/replacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.12 Operators and motions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.13 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.14 Other stuff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.15 Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.16 HELP!! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

    3 Indices and tables 27

    i

  • ii

  • Introduction to UNIX Documentation, Release 1.0

    Contents:

    CONTENTS 1

  • Introduction to UNIX Documentation, Release 1.0

    2 CONTENTS

  • CHAPTER

    ONE

    UNIX BASICS

    We will talk to the computer using the text-based terminal also known as “the commandline”. On a Unix -like operating systems (typically used for high-performance computing) this is a very powerful way to interact with the computer.

    On a typical Linux desktop system you open a “xterm” or “kterm” (or similar application) to get access to the com- mandline. On Mac OS X you open “Terminal.app” (in the Utilities folder in Applications).

    • We use the “bash” shell bash (“Bourne again shell”, which replaces the Bourne shell sh). Bash is a very good shell to write scripts in and to use on an every day basis. Pretty much everything written about sh also applies to bash (including the original and very readable Introduction to the Unix Shell written by Steve Bourne in 1978).

    • Bash is available on most modern Unix-like operating systems; it is the default shell on Linux and Mac OS X.

    • There are other shells out there (like csh and tcsh (“C-shells”) or the Korn shell ksh). We will not deal with them. In particular, the C-shells should not be used for writing scripts, for many good reasons.

    Command syntax: options and arguments:

    command [-argument] [-argument OPTVAL] ... [--long-argument] [--long-argument=OPTVAL] [file ...]

    • single-letter, can be combined

    • long options

    • arguments: if not supplied, often read from “standard input” and output to “standard output”

    1.1 Help

    Not all help functions described below are always available. Simply try them all. You need to learn to find out about commands by yourself. This introduction can only point you in the right direction and give you hints at to what could be useful to you.

    • built in help function:

    command -h command --help command -? command

    • manual page (‘man page’):

    man command man -k search_phrase

    This is where to look when someone tells you to RTFM.

    3

    http://en.wikipedia.org/wiki/Unix http://darthtater.asurite.ad.asu.edu/unix/shell.html http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ http://www.grymoire.com/Unix/CshTop10.txt http://foldoc.org/RTFM

  • Introduction to UNIX Documentation, Release 1.0

    • help function of the shell:

    help command

    • info page (can be somewhat similar to man but can contain hyperlinks):

    info command

    And of course, you can always search on the web. Just make sure you understand which version of the command is described. There can be huge differences. If in doubt, look if a command complies to the “POSIX standard”, a lowest common denominator for various flavors of Unix (yes, there are many different Unix-like operating systems out there!).

    1.2 Navigating file system

    See where you are and move around:

    pwd

    cd # cd to home cd ~ # cd to home: ~ stands for home cd directory cd . # cd to the current dir (mostly useful with -P/-L options) cd .. # cd to parent dir cd - # cd to previous dir (try it out)

    List files:

    ls ls directory ls -l # long format ls -a # hidden files -- try ’ls -la’ ls -ld # directory information -- ’ls -l ~’ vs ’ls -ld ~’ ls -R # recursive

    Excercise

    1. use ls:

    ls ~/Documents ls ~/.. ls -lR / # takes forever, cancel with CTRL+C (= ^C)

    2. create directory space:

    ~/NAME/p01 p01/tmp p01/editor pdb docs

    3. look at these special directories:

    / # "root" of the file system /bin # commands ("binaries") are stored here (needed at system boot) /usr/bin # standard commands stored here /Volumes # Mac OS X specific: disks (and your USB flash drive) appear here

    4 Chapter 1. Unix Basics

    http://en.wikipedia.org/wiki/File:Unix_history-simple.svg

  • Introduction to UNIX Documentation, Release 1.0

    1.3 Copy, renaming, deleting

    • download from http://darthtater.asurite.ad.asu.edu/PHY598/01/ vim_basics.txt

    • copy to your docs directory:

    cp vim_basics.txt docs

    • move file to editor directory:

    mv vim_basics.txt p01/editor

    • make a temporary copy:

    cp docs/vim_basics.txt tmp cd tmp cp vim_basics.txt foo.txt mkdir tmp2 cd tmp2 mv ../foo.txt . # note ’.’ for current dir

    • remove the copy:

    cd .. pwd rm vim_basics.txt

    • remove the tmp dir:

    rmdir tmp2 # only empty dirs! ## FAILS

    rm -r tmp2 # deletes full dirs recursively (dangerous)

    Warning: rm -r and rm -rf (“force”, even override permissions) are very dangerous!! It deletes everything recusively. It does not ask and it does not keep a backup (no “Trash”).

    In general, Unix commands do what you ask them to. If they succeed they typically will not output anything (unless it’s part of their job such as ls). Only if there’s a problem you’ll get a (terse) message.

    1.4 Looking at files

    Different ways to display a text file:

    cat FILE less FILE # h for help, q for quit head FILE tail FILE

    Try head -5 vim_basics.txt. For looking at log files of a running simulation, try

    tail -f output.log

    which will continuously update.

    1.3. Copy, renaming, deleting 5

    http://darthtater.asurite.ad.asu.edu/PHY598/01/

  • Introduction to UNIX Documentation, Release 1.0

    1.5 Shell name generation

    “glob” patterns

    • * means any characters (even zero):

    ls * ls *.txt

    ls /usr/bin/*grep

    • ? means any single one character:

    ls /usr/bin/?grep

    [x-y] means a range:

    ls /usr/bin/[a-y]grep

    • Advanced: brace expansion

    head_{a,b,42,xx}_tail --> head_a_tail head_b_tail head_42_tail head_xx_tail

    Can be useful when making a complicated directory layout, e.g. simulations for three systems S1,S2,S3 at four temperatures 273, 300, 310, 373 and 2 pressures (1 atm and 1000 atm) (3*4*2 = 24 directories):

    mkdir -p {S1,