Summary Up to Shells

Unix Structure – Kernel + Shell

Linux, GNU Project

File structure - inodes

Standard codes:

intr

^c

Stops a program

erase

^h

backspaces

werase

^w

erases last word typed

kill

^u

kills the current input line

quit

^\

stops the program and saves core in a file

stop

^s

pause screen output

start

^q

resumes screen output

eof

^d

no more data

suspend

^z

temporarily stops (i.e., suspends) a program

resume

^y

resumes running a program

 

Unix commands

·         Navigation: cd (. Is this folder, .. is up a folder, ~ is home)

·         info: date, who, who am I, whoami, man, pwd

·         file/ dir  control: mv, cp, rm, chmod, ln, rmdir, mkdir, chown, chgrp, tar

·         display: cat, more, pr, echo, od, du, whereis, which

·         file info: find, ls, wc

·         filters:   egrep, sort, tail, diff, sed

·         processes: ps, fg, kill, wait, (use & at end of command to send back)

Unix Metacharacters

·         * - match anything

·         ? – match any one character

·         \ - escape to convert metacharacter to plain character

Redirect

Ø  > - create file output

·         >> append file output

·         < file input

·         2> - error output redirect

·         | pipe the output into the next command

·         tee – write the current input to a file

·          

Use vi, ed

Ø  Input mode via a, o, i

Ø  Command mode via esc

o   Change text:

§  x – delete a charcter

§  r – replace a character

§  R – replace a word

§  J – join

§  ~ - upper/lower

§  d – delete (and put in buffer)

§  p – put what is in buffer #

§  = - indent according to c program rules in a c program (follow by G to indent to end)

o   Fill Buffer :

§  y – yank

§  c – cut

o   Movement:

§  G – end

§  gg – top

§  line – move that many lines forward

§  h or j or k or l – move cursor

§  0 –beginning of line

§  $ - end of line

§  / - find the string

o   : commands:  Structure is line start, line end, command (other structure exists too)

§  s – substitute one line

§  %s – substitute many lines

§  w – write (if filename write to that one)

§  q – quit

§  ! – don’t give me warnings

§  p - print

Regular Expressions

Atoms

Ø  Single characters

Ø  Dots – any character except new line

Ø  Classes [ ] – any one inside or [^ ] – not any one inside

Ø  Anchors – where in text - ^ begin line, $ end line, < begin word, > end word

Ø  Back references - \# matched text from a subset ()

Operators

Ø  Sequence

Ø  Alternation | this or that

Ø  Repetition: * (0 or more); + (1 or more); ? (0 or 1); {start,end}

Ø  Group: () subset for backreference; repeat group sequence

Ø  Save

Use with Grep

Ø  Searches for the regular expression in a file or from a piped output

Ø  Shows only matched lines

Ø  Options: -v reverse (not found match); -n line numbers; -i ignore case; -c count lines

        Use with SED

Ø  Edits a stream with essentially vi commands

Ø  -i  to edit file contents

Ø  -r to use extended regular expression syntax

Ø  -f execute the sed commands in the script, piping each output into the next line

Ø  -e sequence of commands with –e before each

Ø  -n suppress all output (so that you can specify printing with p in sed command)

Ø  { ; ; } set of commands to run when address matched (a little like –e)

Ø   Commands

o   Most commands that are available in vi

o   x – swap pattern space and hold buffer

o   = line number 

o   c – change a cline

o   a – append after cursor

o   i  - insert

o   d – delete

o   p – print

o   s – substitute

o   y- transform (character set mapping)

Filters : 

sort – sort all input in order by some key : 

·         -f fold upper and lower case together

·         -n numeric order instead of text order

·         -k# column number to sort by

·         Example: sort –nk2 file1 file2

·         Remember it will sort as many files as you give so: sort filea fileb filec puts all 3 files together in a sorted list

uniq  - deal with duplicate lines :

·         -c tells how many times a line is repeated

·         -d print only lines that are duplicates of the lines above them

·         -u prints only lines that are not the duplicate of the line above them

·         Example: sort file1 file2 | uniq -d

·         Remember to sort when you want duplicates anywhere within the list considered

comm – compare files listing files in columns next to each other

if given 2 sorted lists, it will create 3 columns: (Use –column number to suppress a particular column)

·         1 : only in the first file given

·         2 : only in the second file given

·         3 : those in both files

·         Example: comm -3 <(sort -n csc270file) <(sort -n csc271file)

tr – translates characters from one given set to another set

·         tr a-z A-Z file : makes uppercase

·         tr ab 12 file : changes all a’s to 1 and b’s to 2

·         tr –c ab 1 file : changes everything but a and b to 1

·         tr –s ab 12 file : changes all a, aa or a…a to 1 and b,bb or b..b to 2

Finding files:

·         Find a file in a a subdirectory:

o   du -a | grep yourfile

o   find . -name *yourfile*

·         Only list files in current dir

o   find . -maxdepth 1 -type f

o   ls -p | grep -v / | column

§  p writes a slash after all filenames

·         Only list subdirs in current dir

o   ls -ld */