René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Shell Tips

Substrings of Variables

In order to print substrings of variables, use the ${var:start:len} notation.
var=something
echo ${var:4:3}
prints thi. This works with BASH, I don't know about the other shells.

Finding the most recently changed file in a directory

The following line opens the most recently changed file in the current directory with vim:
vim $(ls -lrtF | grep -v '^d' | tail -1 | awk '{print $8}')
Based on this mechanism, a function can be created that does a command on the most recent file. The function is hence called mrf:
mrf () {
  typeset f=$(ls -ltrF | grep -v '^d' | tail -1 | awk '{print $8}')
  if [ "$1" == "" ]; then
    echo $f
  else
    $* $f
  fi
}
If the function is called without arguments, it just prints the name most recent file's name. If it is called with arguments, it assumes those arguments are a unix command with possible options. For example, it's possible to chmod the newest file:
mrf chmod 700