WorkingOnServers/UNIXHints
<rst> Here we will create a collection of useful resources for surviving in a UNIX world...
- Shell commands**
A good `tutorial <http://www.hypexr.org/bash_tutorial.php>`_ on using and customising the *BASH shell*
The BASH shell `reference manual <http://www.gnu.org/software/bash/manual/bashref.html#SEC_Top>`_
- Looping (Bash)**
Print out all files beginning with 2008.
for i in 2008*; do echo $i done
- Chopping strings (Bash)**
Find the filename and the pathname
basename /usr/local/bin/matlab/bin/readme.txt pathname /usr/local/bin/matlab/bin/readme.txt
will output
readme.txt /usr/local/bin/matlab/bin/
- Subdividing a variable**
- <String> finds the longest substring matching <String> from the beginning
%%<String> finds the longest substring matching <String> from the end
${<Variable>:<begin>:<end>} takes the substring indexed by <begin> and <end>
a="PeterPaulAndMary.html" echo ${a##Peter} echo ${a%%.html} echo ${a:5:4}
will output
PaulAndMary.html PeterPaulAndMary Paul
- Moving multiple files**
Move all grid_time_* to grid_time_*_001
find grid_time* -exec mv {} {}_001 \;
- Removing empty files**
find -size 0 -exec rm {} \\;
- Password-free login**
- Generate a public/private key pair:
local> ssh-keygen -t dsa -f .ssh/id_dsa
-t tells the type of encription
-f tells where to store the public/private key pairs
press <enter> when asked for password
- Copy the public key to the server machines:
local> cd .ssh local> scp id_dsa.pub user@remote:~/.ssh/id_dsa.pub
- Add the client's public key to the known public keys:
remote> cat id_dsa.pub >> authorized_keys2 remote> chmod 640 authorized_keys2 remote> rm id_dsa.pub remote> exit
</rst>