3

Tip #145   swap files

# Swap 2 files/dirs that are in the same dir
# Usage: sw f1 f2
function sw {
f1=$1
f2=$2
if [ "x$f2" = "x" ]; then
echo "Usage: sw file1 file2"
echo " swap name of 2 files"
else
d1=$(dirname $f1)
d2=$(dirname $f2)
if [ "$d1" != "." -o "$d2" != "." ]; then
echo "sw: Can swap only files in current directory"
else
if [ -e "$f1" -a -e "$f2" ]; then
mv $f1 .sw.$f1
mv $f2 $f1
mv .sw.$f1 $f2
else
echo "sw: '$f1' and '$f2' must exist"
fi
fi
fi
}