Get the Directory Path of a Bash Script

Posted by Chief on Jun 14, 2010 in Scripts, Tricks and Hacks
No Comments

So you have a bash script and you want to run it from crontab or some other context where the current working directory isn’t known. Your script wants to use relative path information (from the script itself), perhaps to create a directory structure in the script’s base directory.

The question is: How do you get the directory path of the script? dirname? Not reliably.

#!/bin/bash
ABSPATH="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"
CWD=`dirname "$ABSPATH"`

Nice!

Tags: , , , , , ,

Recursive directory merge

Posted by Chief on Mar 11, 2010 in Reference, Scripts, Tricks and Hacks
No Comments

Problem: I have two directories that are similar in structure but different in content. Say one is an older snapshot of the other. I want to copy the stuff from the new directory into the old directory (recursively).

Copy:
rsync -a SOURCE/ DEST/ --ignore-existing --whole-file

Move (no clobber):
rsync -a SOURCE/ DEST/ --remove-sent-files --ignore-existing --whole-file

Move (and clobber):
rsync -a SOURCE/ DEST/ --remove-sent-files --whole-file

Move, but only update with newer:
rsync -a SOURCE/ DEST/ --remove-sent-files --update --whole-file

Tags: , , , , , , , ,

Copyright © 2010 cat brain.log | less All rights reserved.
Shades v1.2 theme from BuyNowShop.com.