Introduction to Unix - ACCRE Vanderbilt · Introduction to Unix 2 Outline Overview of the Unix...
Transcript of Introduction to Unix - ACCRE Vanderbilt · Introduction to Unix 2 Outline Overview of the Unix...
Introduction to Unix
2007 May
http://www.accre.vanderbilt.edu
Introduction to Unix 2
Outline
Overview of the Unix Operating System (slides 3-4)
Shell, CL, Basic Ops, Help, Special Characters (slides 5-15)
Files, Directories, & Access Rights (slides 16-17)
Defining Your Environment (slides 18-24)
Text Editors (slides 25)
Input/Output Redirection - Pipelines & Filters (slides 26-38)
Processes & Multitasking (slides 39-40)
Scripting (slides 41-44)
Compressing & Archiving (slides 45-46)
Getting Help from ACCRE (slide 47) ; O’Reilly books (slide 48)
screen, a nice Unix tool (slide 49)
Introduction to Unix 3
What is UNIX?
UNIX is an operating system like Windows orMac OS X
UNIX was originally created in 1971 by KenThompson and Dennis Ritchie of AT&T BellLabs
It was designed to be a portable, multitasking,multiuser operating system
UNIX is a “standard”, i.e. there isn't *a* UNIX,but multiple implementations of the UNIXdesign (Linux, BSD, Solaris, AIX, OS X)
Introduction to Unix 4
All UNIX's aren't the same
All UNIX's share a common design philosophyof simplicity
Each UNIX usually develops its own niche orspecialization
Mac OS X: “User-friendly” desktop• Red Hat Linux: “Easy” workstation, simple server
• Debian Linux: “Advanced” workstation and server
• FreeBSD: High-performance server
• OpenBSD: Security and network applications
Introduction to Unix 5
Unix Shell and CL The shell is an interpreter used to communicate with
the OS interactively on the command line (CL)
The cluster has two “flavors”: tcsh and bash. Onlinemanuals and FAQ:
http://www.gnu.org/software/bash/
http://www.tcsh.org/Home
For historical reasons, tcsh is the default shell for allusers on VAMPIRE (the ACCRE cluster)
If you wish to change your default shell, use the chshcommand on vmpsched
CL prompt> chsh -s /bin/bash
Introduction to Unix 6
CL Operations and Files
There are Unix commands (same function inany shell) and shell built-in commands
Commands entered at the CL prompt haveoptions and arguments
Files and directories are the primary abstractionin UNIX (similar to icons and folders inWindows)
Directories are files with information on theircontents
Some useful Unix commands for interactingwith files and directories:
Introduction to Unix 7
Basic Commands
• pwd - print working directory• ls - list contents of directories• mkdir - make (create) new directories• cd - change the current directory
• cp - copy files or directories• mv - move files or directories• rm - remove (delete) files or directories
• cat - concatenate file contents• more/less - scroll file contents• file - show file type
simple bash examples with CL editing
Introduction to Unix 8
Command Help
Unix traditionally includes instructions and helpfiles (manual pages) on most commands andAPI's and their options. To access a manpage:
CL prompt> man foo
For csh/tcsh and sh/bash built-in commands:
CL prompt> man builtin
Some commands have command line help (usagehints or --help)
Other sources of help: /usr/share/doc, Google
Introduction to Unix 9
Basic CL & Editing• <tab> complete filenames and commands on CL• up/down arrows scroll CL history• left/right scrolls back and forth on CL• history shell command lists CL history (also tcsh)
functionCL (event) repeat
(function described on next slide):
repeat most recent, but substitute new forfirst old^old^new^
repeat nth event (see history)!n
repeat nth previous event!-n
repeat last containing string!?string
repeat last starting with string!string
repeat previous command!!
Introduction to Unix 10
Basic CL & Editing
• “:” allows more complex CL editing, e. g., selectlast event containing specific word, replace word,then execute command:
CL prompt> !?old:s/old/new/
This repeats the last event containing old, butsubstitutes new for old
Introduction to Unix 11
Basic CL & Editing
temporary interrupt (can also send to background, slide38)
^Z
cancel current CL, return prompt^C
delete under cursor^D
delete to end of line^K
back 1 word<esc>B
back 1 character^B
forward 1 word<esc>F
forward 1 character^F
end of line^E
beginning of line^A
next line^N
previous line^P
function on command linekeystroke
Introduction to Unix 12
Files, Directories, SpecialCharacters
Files to care about• hidden (filenames begin with a dot, list with ls -a),
e. g., user initialization, .login, .bashrc, .cshrc, …• global initialization, e. g., /etc/bashrc• devices• symbolic links
/ = top root directory
A few special Unix shortcuts for file/directory names/paths:• ~ = expands word to your home directory path• ~username = home directory of any username• . = current directory• .. = parent directory
Introduction to Unix 13
Files, Directories, SpecialCharacters
Blank space
Shell executes command in background (slide 31)&
Quote following special character as normal string(e. g., filnames containing spaces, or slide 17)
\
Examples throughout presentation (esp. scripts)“ ` { } #
Separates commands (see slide 15);
more special characters
Used within [] denotes range of characters-
Enclose set of characters, match any one by position[ ]
Match any single character in filename?
Match any character in filename*
wildcards
N. b., best to avoid using these characters in filenames
Introduction to Unix 14
Files, Directories, SpecialCharacters
E. g., globbing (expanding wildcard to match pattern):
CL prompt> ls -1 *file1file2~file3
CL prompt> ls ?ile1file1
CL prompt> ls file[1-2]file1
CL prompt> ls file[a-z0-9]~file2~
Introduction to Unix 15
Multiple commands and ;
You can use the “;” operator to append manycommands on one line:
CL prompt> date ; uptime
Tue May 8 11:41:26 CDT 2007
11:41 up 8 days, 22:26, 1 user, load averages:0.81 0.69 0.67
Introduction to Unix 16
File Permissions
Files/directories have an owner and a group
The owner can grant read/write/executepermissions to three groups (the user, the user'sgroup, and all others on the system)
prompt> ls -l /home/usernametotal 16drwxr-xr-x 2 username group 8192 May 1 15:59 certs/drwxr-xr-x 3 username group 8192 Sep 15 17:28 classes/
Introduction to Unix 17
File Permissions
These permissions are modified by the chmodcommand, e. g. :
CL prompt> chmod g+rx file
permits other users in the owner’s group toread and execute file
Introduction to Unix 18
Startup Files, Variables, Aliases
Two types, environment and shell, can be(re)initialized at any time.
If defined in .login, environment variablesare set upon login
If defined in shell run command files (e. g.,.bashrc, .cshrc, .tcshrc), shellvariables set upon each instance of shell
Frequently used variables should be set inthese files
Introduction to Unix 19
Startup Files, Variables, Aliases
default when spawned by other programsEDITOR
login nameUSER
terminal typeTERM
default printerPRINTER
directories containing commandsPATH
operating systemOSTYPE
name of computerHOST
home directory pathHOME
screen for X windows displayDISPLAY
machine architectureARCH
functionname
A few common variables common to both shells:
Introduction to Unix 20
Startup Files, Variables, Aliases
Important environment variables:
• PATH - points to executables
• LD_LIBRARY_PATH – points to libraries
Reference a variable with $ or ${}
echo and printenv commands:
CL prompt> echo $PATHCL prompt> echo ${PATH}CL prompt> printenv PATH
Introduction to Unix 21
Setting Variables
To assign a value to a variable (shells have different syntax):
Example of adding to your PATH:
rehash lets shell know PATH was just updated
set FOO = “bar”
setenv FOO “bar”tcsh
export FOO=bar
FOO=bar ; export FOObash
set path = ( $path $home/bin:/directory2 )
setenv PATH “${PATH}:${HOME}/bin:/directory2”tcsh
export PATH=$PATH:$HOME/bin:/directory2bash
Introduction to Unix 22
Setting Variables
Example of setting a new variable to the outputvalue of a command:
CL prompt> current_date_time=`date`CL prompt> echo $current_date_timeMon Apr 23 14:15:35 CDT 2007
Another example (note pwd vs. $PWD):CL prompt> echo "I am in `pwd` on $HOST”I am in /home/username on vmps08CL prompt> echo "I am in $PWD on $HOST”I am in /home/username on vmps08
Introduction to Unix 23
Setting Command Aliases
Shorten frequently used or lengthy commands:• tsch syntax:
alias rm 'rm -i’
• bash syntax:alias rm='rm -i’
• other useful aliases:alias cp='cp -i'alias mv='mv -i'alias ls='ls --color-tty -F'alias ll='ls -laF'
Introduction to Unix 24
Setting Command Aliases
Return to default:
• “backslash” temporarily returns to default:
\rm junk
• unalias returns default for rest of session:unalias rm
rm junk
Introduction to Unix 25
Text Editors on Cluster
Fairly basic:
Nano (http://www.nano-editor.org/)
More advanced:
Vim (http://www.vim.org/)
Emacs (http://www.gnu.org/software/emacs/)
Create/edit a new file example.txt
Introduction to Unix 26
Input/Output Redirection
Most programs have three I/O streams:
• stdin – standard input
• stdout – standard output
• stderr – standard error.
They all default to the console ("console"means the keyboard for the input and thescreen for the output)
Introduction to Unix 27
Input/Output Redirection
To redirect stdout of a program to a file:bash: myprogram 1> output.log
tcsh: myprogram > output.log
To redirect stderr of a program to a file: bash: myprogram 2> error.log
To redirect both stdout and stderr to same file (ordermatters): bash: myprogram > combined.log 2>&1
To redirect both stdout and stderr separately:(myprogram >output.log) >&error.log
Introduction to Unix 28
Input/Output Redirection
( comm > ofile ) >& efilestdout/err to 2 files
comm < ifile > ofile 2> efilestderr to third file
comm < ifile > filecomm < ifile > fileredirect stdin/out
comm <<ccomm <<cstdin until “c”
comm >> ofile 2>&1comm >>& ofilestdout/err to end
comm 2>> ofilestderr to end of file
comm >> ofilecomm >> ofilestdout to end of file
comm < ifilecomm < ifilestdin from file
comm >ofile 2>&1
comm &> ofilecomm >& ofilestdout/err to file
comm 2> filestderr to file
comm > filecomm >ofilestdout to file
bashtcshfunction
Introduction to Unix 29
Input/Output Redirection
Example of “>>” operator to appendinformation to a file:
prompt> date > foo
prompt> cat fooWed Aug 31 17:27:52 CDT 2005
prompt> date >> foo
prompt> cat fooWed Aug 31 17:27:52 CDT 2005Wed Aug 31 17:27:56 CDT 2005
Introduction to Unix 30
Input/Output Redirection
Use of single back quotes to substitute in thevalue of another command:
Compare to slides 14 and 22:
prompt> ls `find . -name “file[a-z0-9]~”`file2~
prompt> echo `find . -name “file[a-z0-9]~”`file2~
Introduction to Unix 31
Input/Output Redirection
script to redirect CL stdin/out to recordsession to a file (however, formatting not sonice)
To redirect output to “nowhere” use the nulldevice, /dev/null
To redirect stdout to null device (tcsh/bash):myprogram >/dev/null
To redirect stdout and stderr to null (bash):myprogram >/dev/null 2>/dev/null
Introduction to Unix 32
I/O Redirection - Pipes/Filters
Pipelines are a set of processes chained by their standardstreams, so that the stdout of each process feeds directlyas the stdin of the next.
Pipelines are defined using the “|” character.
E. g., use a pipe and tee to direct output of echo toboth stdout and to a file:
prompt> echo "Hello World" | tee output.txt
comm 2>&1 | comm2comm |& comm2pipe stdout/err to comm2
comm | comm2comm | comm2pipe stdout to comm2
bashtcshfunction
Introduction to Unix 33
I/O Redirection - Pipes/Filters
Commands can be used as filters which take the outputof a program and modify it. E. g., use a pipe to countwords from the output of echo:
prompt> echo "Hello World" | wc -w2
Very useful filters include:• grep - Pattern matching• sed - Search and Replace• cut - Print specific columns• sort - Sort alphabetically / numerically• uniq - Remove duplicate lines from a file
Introduction to Unix 34
I/O Redirection - Pipes/Filters
grep example:
prompt> cat example.txtHello WorldGoodbye World
prompt> cat example.txt | grep HelloHello World
prompt> cat example.txt | grep -v HelloGoodbye World
Introduction to Unix 35
I/O Redirection - Pipes/Filters
sed example:
prompt> cat example.txtHello WorldGoodbye World
prompt> cat example.txt | sed “s/Hello/Goodbye/g”Goodbye WorldGoodbye World
Introduction to Unix 36
I/O Redirection - Pipes/Filters
cut example:
prompt> cat example.txt1,Hello,World2,Goodbye,World
prompt> cat example.txt | cut -d "," -f 2-Hello,WorldGoodbye,World
Introduction to Unix 37
I/O Redirection - Pipes/Filters
sort example:
prompt> cat example.txt2 Goodbye1 Hello2 Goodbye
prompt> cat example.txt | sort -n1 Hello2 Goodbye2 Goodbye
Introduction to Unix 38
I/O Redirection - Pipes/Filters
uniq example:
prompt> cat example.txt2 Goodbye1 Hello2 Goodbye
prompt> cat example.txt | sort -n | uniq1 Hello2 Goodbye
Introduction to Unix 39
Processes and Multitasking
To run a program in the background, use the “&”character (or “^Z” followed by “bg”):
prompt> myprogram &[1] 7895
myprogram is now running in the background asprocess id (PID) 7895
Whenever your process finishes, it will print“Done” to the console.
Introduction to Unix 40
Processes and Multitasking
To check on the status of your jobs running onthe system, use the ps command
prompt> ps -a PID TTY TIME CMD 8095 pts/3 00:00:00 ps
You can get an expanded list by typingps agux, or by using the top command
Use uptime to check the load average (howhard system is working) on slowly respondingmachines
Introduction to Unix 41
Simple Shell Scripting
String unix commands into a shell script.#/bin/csh N. b., not a comment on line 1
To execute:tsch: prompt> source myscript01.csh
bash: prompt> . ./myscript01.sh
Or run as executable:prompt> chmod u+x myscript01.csh
prompt> myscript01.csh
prompt> ./myscript01.csh (if not in path)
Introduction to Unix 42
Simple Shell Scripting
Simple bash programming: myscript02.sh#!/bin/sh# define variablelist="Mercury Venus Earth Mars Jupiter Saturn Uranus
Neptune”# initiate counteri=1# for loopfor planet in $listdo echo "planet $i is $planet" # Print to STDOUT i=`expr $i + 1` # Increment counterdone
Execute it:prompt> chmod u+x myscript02.sh ; ./myscript02.sh
Introduction to Unix 43
More on Variables
Can also define conditional variables:
If VAR, use its value else usealternate_value and exit if in shellscript; if alternate_value also emptyprint print msg to stderr
${VAR:?alternate_value}
If VAR, use alternate_value${VAR:+alternate_value}
If VAR, use its value else usealternate_value and set VAR=alternate_value
${VAR:=alternate_value}
If VAR defined, use its value else usealternate_value${VAR:-alternate_value}
resultformat
Introduction to Unix 44
More on Variables
Example bash uses of conditional variables:
prompt> echo $VAR $ERROR
prompt> echo ${ERROR:?”An error was found”}bash: ERROR: An error was found
prompt> ERROR=TRUE
prompt> echo ${ERROR:+”An error was found”}An error was found
prompt> echo ${ERROR:-”An error was found”}TRUE
prompt> echo ${ERROR:=”An error was found”}TRUE
Introduction to Unix 45
Compressing and Archiving
There are several ways you can compress files toreduce disk usage or transfer time.
The “Windows” way is using the zip and unzipcommands:prompt> ls -alh testfile1 testfile2-rw-r--r-- root root 1.0M testfile1-rw-r--r-- root root 1.0M testfile2
prompt> zip testfile.zip testfile1 testfile2
prompt> ls -alh testfile.zip-rw-r--r-- root root 2.4K testfile.zip
Introduction to Unix 46
Compressing and Archiving
Traditional UNIX archiving tools, tar and gzip.
tar takes a number of files/directories andcombines them into a single file
gzip takes combined archive and compresses it:
tar -c file1 file2 ... | gzip -9 > archive.tgz
Or simply:tar -zc archive.tgz file1 file2 …
To extract files from a tar archive:tar xfzp archive.tgz
Introduction to Unix 47
Getting Help from ACCRE
ACCRE website FAQ and Getting Started pages:
www.accre.vanderbilt.edu/support
ACCRE helpdesk:
www.accre.vanderbilt.edu/support/contact/submit_RT.php
accre-forum mailing list
Office hours at ACCRE M-F 4-5 PM
Introduction to Unix 48
Give UNIX a try!
There are several free flavors of UNIX available:
•Fedora: http://www.fedora.redhat.com
•Debian: http://www.debian.org
•FreeBSD: http://www.freebsd.org
•Ubuntu: http://www.ubuntu.com
O’Reilly has excellent desktop reference materials (I. e., books):
http://www.oreilly.com/
(E. g., “Linux in a Nutshell”, “Class Shell Scripting”, …)
O’Reilly pocket guides also very useful quick references (Linux OS,shells, editors, …)
\
Introduction to Unix 49
Screen
Another useful unix tool with remote session Tutorial :
… Log onto cluster noting the gateway (i. e., vmpsxx) …prompt> screenprompt> nano junk.txt
… While in editor, close window …
… Must log back onto node you were on, i. e.,vmpsxx.accre.vanderbilt.edu …
… Find SCREEN process ID (PID) …prompt> ps augx | grep <your user name>prompt> screen -R <PID>
… Your process is Restored where you left off …