UNIX / Linux Primer

17
UNIX / Linux Primer July 22, 2008 Nicholas F. Polys Ph.D.

description

UNIX / Linux Primer. July 22, 2008 Nicholas F. Polys Ph.D. UNIX. Many flavors Enterprise / Industrial strength kernel Macs are based on BSD Unix Under Win, install CygWin Linux and Open Source are ‘Free’ as in Puppy- not ‘Free’ as in beer . The Command Line. - PowerPoint PPT Presentation

Transcript of UNIX / Linux Primer

Page 1: UNIX / Linux Primer

UNIX / Linux Primer

July 22, 2008

Nicholas F. Polys Ph.D.

Page 2: UNIX / Linux Primer

UNIX

• Many flavors

• Enterprise / Industrial strength kernel

• Macs are based on BSD Unix

• Under Win, install CygWin

• Linux and Open Source are

‘Free’ as in Puppy- not ‘Free’ as in beer

Page 3: UNIX / Linux Primer

The Command Line

• Accessible through the wire

• Real power to control the system through a terminal running a shell

• Typical forms:$ somecommand –optiona value –optionb value

$ somecommand –optionaoptionb

• Cntrl-c will quit a running command

Page 4: UNIX / Linux Primer

Commands

• When you log in, you are in a ‘shell’

• You interact with the system by typing at the prompt or running scripts

• To find out the options for a command, use manual e.g.

$ man somecommand– :q will quit (like vi)

Page 5: UNIX / Linux Primer

The Filesystem

• Your home directory is ~username– Typically /home/username

• pwd shows your current directory• ls –al list files in current dir• mkdir dira make a dir• touch filea create a file• mv filea fileb move• cp filea fileb copy• diff filea fileb difference• rm … remove …careful!

-f forces it –r means recursive

Page 6: UNIX / Linux Primer

Permissions

• Files and folders can be owned and permissions set

• chmod a+x targeta– user, group, other, all– +, -– read, write, execute

Page 7: UNIX / Linux Primer

Inspecting files

• more filea show the whole file

• less filea show one screen atat– :q will quit (like vi)

• tail filea show the last screen

Page 8: UNIX / Linux Primer

Environment Variables

• Your account has a default shell; env variables can be set there (for the bash shell,

e.g. ~username/.bashprofile or .bashrc

• Env shows your current settings such as your PATH

• You can set you own env vars for a shell session with:

export SOMEVAR=value

Page 9: UNIX / Linux Primer

Processes• ps –ef show active processes• kill -9 pid kill the process with pid• mozilla & add ‘&’ to the end of a command to

run it in the background (and keep your command prompt)

Compression• tar wrap up multiple files/folders .tar

-cvf wrap -xvf unwrap (force, verbose)• gzip ; gunzip (.gz)

Page 10: UNIX / Linux Primer

Vi editor

• Text editing on the remote files system (through a terminal shell)

• Text editor for shell / keyboard interaction– Insert vs Command mode <esc>– See basic usage handout– Try it out– Takes practice

Page 11: UNIX / Linux Primer

Other Typical Apps

• cat – concatenates files

• awk – manipulate rows and columns of data efficiently

• grep / egrep – searching files for patterns (aka ‘regular expressions’)

Page 12: UNIX / Linux Primer

Pipes

• | send a stream of data

• > write a stream of data to file

• >> append a stream of data to file

e.g.

• ps -ef | grep ‘npolys’

• ls –al > filea.txt

Page 13: UNIX / Linux Primer

Shell scripts

Collect commands and env vars so you don’t have to type them every time

• Typically use .sh file extension

• Invoke with ./myscript.sh

• File begins with #!/bin/sh

• Perl – begins with #!/usr/bin/perl– Find where perl is: which perl

Page 14: UNIX / Linux Primer

Connecting to other machines

• Shell– ssh [email protected]

• Files– secure copy (remote file copy program)

• scp

– interactive secure ftp sessions• sftp [email protected]

– get file– put file– ‘cd’ versus ‘lcd’

Page 15: UNIX / Linux Primer

Compilation

• gcc GNU C and C++ compiler

• Most apps have a configure script and a Makefile which automates compilation and installation; always check the README

• Makefiles

Page 16: UNIX / Linux Primer

Debugging

• gdb

• Requires flags during compilation (-g) which will create an a.out file

• ‘gdb a.out’ will begin the debugger

• See– http://sourceware.org/gdb/current/onlinedocs/

gdb.html

Page 17: UNIX / Linux Primer

Profiling

• gprof • Requires flags during compilation (-pg)

which will create an a.out file• Run the app once (eg $> ./a.out )• Then ‘gprof a.out’ will begin the profiler,

showing timings and the application’s call graph

• http://sourceware.org/binutils/docs/gprof/index.html