Friday, August 21, 2009

Relative to Absolute Path in Shell Script

Simple bash function to convert relative paths to absolute pats
{ echo “`cd \`dirname $1\`; pwd`/`basename $1`” }

UPDATE: unfortuately the above only works for directories that exist - the perl hack will get around this

{FILE=`$PERL -e "use File::Spec::Functions qw[rel2abs];print rel2abs('$1');"`; echo "$FILE"; }

Thursday, August 20, 2009

Get File Extension in Shell Script

Returns the last file extension in the file name
echo "thisfile.txt.log"awk -F . '{print $NF}' # returns "log"