Unix is nice.
-it works with simple modules rather than pre-built macros so
that you have a chance to understand what went wrong
-it can be automated
-it is neutral
-it is universal
-you can get quite fast not switching constantly between
keyboard and mouse
-people admire you using it (looks cool!)
SHELLS
=======
different shells: csh, bash, Bourne shell
#!/bin/sh/
PIPES
======
tee log.file copy output to log.file
x>> file append x to file
cat << WORD > y
blabla
WORD take everything from WORD to WORD as input for y
&0 file name (programme name) input
&1 stdd in
&2 stdd out
&3 error msg
2>
1>
$var environmental variable
set show all env. var.
export VARNAME=/bla/bla use VARNAME for bla...
printenv
$VARNAME usage
$1,$2,$3,... var.s in programme (order of appearance)
$* list of all vars
$# number of all vars
FIND
======
+ /- older/younger
-atime access time
-ctime creation time
-mtime modification time
-name name pattern
-size size of files
NOTE: all special characters (*,&..) need to be
preceeded by \.
find . -atime +150 -print
find all files (including .*) in current directory which access time
is longer than 150 days back
find * -ctime -7 -print
find all files (EXcluding .*) in current directory which have been
created during last week
rm -i `find . -name \*.old -print` OR
find . -name \*.old -print | xargs rm -i
remove (with confirm) all 'old' files
find . -size 0 ! -name temp\* -print
find all zero size files which to not start with 'temp'
GREP
=====
options:
-i case insensitive
-v inverse
-c count instances
-l just list file names with expression
use egrep for advanced searches (just always use it if you have a fast
computer)
regular expressions:
see perl for *.+?^$
| or
() for grouping
A{n,m} from n to m repetitions (use A\{n,m\} in grep)
\< or \> match start or end of a word (only in some
grep implementations)
EXIT CODES
==========
$? ask for
0 everything alright
non-zero error
bla | echo $?
MAKE
=====
prepare Makefile to summarise commands/file links for programme
installation
Makefile (example):
-------------------------------------------
SHELL=/bin/sh
#use var for shell dir
COMPILE=cc
OPTS=-options
LINK=linker
#define variables (extensively!)
princess: crown.o tadpole.o
$(LINK) crown.o tadpole.o -to princess
#target file: list of tiles upon which it depends
#followed by sequence of commands to update the target file
(inset by TAB!!!)
crown.o: crown.frog
$(COMPILE) $(OPTS) crown.frog
tadpole.o: tadpole.frog
$(COMPILE) $(OPTS) tadpole.frog
test.log: princess test.data
dat > test.log
princess < test.data >> test.log
.frog.o:
$(COMPILE) $(OPTS) $<
#$< is a macro that expands to the name of the source file
#which needed recompilation
.SUFFIXES:
#define list of used file suffices empty
.SUFFICES:.o .forg
#redefinde list of suffices, generated files first
#original source files last
#end of Makefile
---------------------------------------------
$ {at} expands to the name of the current target, ie the file
that is to be re-created
$< expands to the name of the "pre-requisite" file, ie
the source file that had been seen to have a newer time-stamp
than the target
$* is like $< except that what it expands to does not
include the file suffix.
touch "touch" file: set access date to current date
RCS and BACKUPs
==================
revision control system
mkdir RCS in you home dir
copy from there from time to time to other location
ci file.fil
#checks in file into RCS dir (backup, version 1.1,...)
co file.fil
#checks out file (re-read file)
co -l
ci -l
#permits use for ONE user (from co until next ci), file
is locked for other people
co -p file
#print to screen
co -r1.4 file
#check out special version
look at cvs
JOB CONTROL
===========
jobs, ps, kill %x
ps
jobs
#show jobs
xterm -fn 10x20 -sb -sl 3--&
# launch new command window (big font, 300 lines scroll back)
xclock&
#clock
at -f command.file noon tomorrow
# execute command.file at noon
at 02:00am
cd /home/project/project
make
^D
#(prompts after each line)
HISTORY
=========
!& last word last command
!! last command
!-n n-th command (count back)
!n n-th command from zero
!string last command which starts with string
!?string? last command containing string
!! :s/e/o/ substitue e to o in last command
^e^o^ same
FILE OPERATIONS
===============
tr 'bla' 'blu' < file
----------------------
#translate blu to bla (substitute)
tr -d '\015\032' < file
# delete "bad" newlines from DOS/Windows files
tr -s ' ' ' \012' < file
# remove redundant blanks (' ') and newlines (012)
sort
----
-t separator option
-k x,y sort by fields x through y
-n numeric sorting
-r reversed
-u unique - remove duplicate lines
sort -t : -k2n -k1 < input.file
#: is separator here (default is tab)
cut
----
-f field list
-c column list (characters, horizontally)
-d delimiter (default is tab)
sort -t : -k1 | cut -d : -f 2
sed
------
#similar syntax as UNIX or Perl
#crude editor BUT can to batch processing for you:
...|sed '12,$s/dog/cat/g;s/egg/bird/g' | ...
#substitutes in files everything except first 12 lines
#until eof ($)
#g is for global substitution
#can use reg.expressions as addresses
/from/,/to/d
#delete
/WORD/,/^ *}$/d
#delete everything from WORD until line with
#possibly some spaces and a single } at the end
#One way in which SED differs from grep is that you can enclose
#a section of a pattern within backslashed parentheses,
#and the effect is that whatever matches that pattern is
#captured and stored in a text variable.
#Refer to them as \1 to \9.
eg:
s/ *\([^]*\) *\([^}*\) / \2 \1/
#which inverses two columns by ()
diff
-------
compare two files
-i ignore upper/lower case distinctions
-b ignore multiple spaces/newlines
-r run recursively through given dirs
-Dsymbol merge files and insert Symbol where discrepancies
occured
-e generate simple edit script (using ed)
which converts file1 to file2
PERL
=======
#big and powerful version of grep
perl -n -e 'print if /regexp/' < somefile.txt
-e pass prgr to perl (execute)
-n do on every line of input
1. on file/dir maniputlation
-----------------------------
-r $file true if file exists & readable
-w $file true if file exists & writable
-e $file true if file exists
-z $file true if exists but zero size
-d $file true if dir not file
and so forth
2. launching sub-processes
-----------------------------
system "javac testfile.java";
#If you want perl to process the output, put command into
#backquotes
{at} grepresults=`grep WORD *.txt`;
3. on regular expressions
-------------------------
4. nice code
----------------
chomp {at} grepresults;
foreach $line ( {at} grepresults){
($linenumber,$contents)=split (/:/,$line);
}
OR
chomp ( {at} words=<STDIN>);
foreach $word( {at} words){
$count{$words}++;
}
foreach $word (keys %count){
print "$word has been counted $count{$word} time\n";
}
OR
while (<STDIN>){
($fifthvalue)=(split /\t/)[4];
($firstname)=split (/,/,$fifthvalue[1]);
}
VI
====
#for your sadomasochostic days... or if emacs doesn't work
^u move up page
^d move down
ESC escape to command mode
x del char
D del line
/search search
?search search backwards
n find next match
/x/y/ substitute
%s/x/y substitute globally
:q! abort editing
ZZ save and exit
WEBSITES
========
http://sunsite.doc.ic.ac.uk/Mirrors/ftp.cygnus.com/pub/gnu-win32/gun-win32/
http://sunsite.doc.ic.ac.uk/packages/CPAN/
------------------------------------------------------------------------------------------------------------------------------------
Christine Vogel
Jan. 2002
from A.Norman's lecture on UNIX tools (CS Dep. Univ.Cambridge)