#!/bin/bash # moves a directory and then links it back # good for moving directories from one partition to another but without changing any settings module_load copy module_load path module_load confirm mvlnbk () { # Show usage and exit with status help () { echo 'usage: mvlnbk -h,-r -- ' } if [[ $1 == "-h" ]]; then help; return; fi local co; local dr="echo " if [[ $1 == "-r" ]]; then co="-r"; unset dr; shift; else echo this will be a dry run, use -r to actually run the command fi if [[ $# -lt 2 ]]; then help; return 1; fi # Check for directories if [[ ! -d "$1" ]]; then echo source $1 not a valid directory help return 2 fi if [[ ! -d "$2" ]]; then echo destination $2 not a valid directory help; return 3 fi # Get paths local src=$(abs_path "${1%/}") local psrc="$(dirname $src)" local dest=$(abs_path "${2%/}") local dir=$(basename $src) local ddir=${3:-$dir} local use_sudo if [[ $(stat -c %u "$psrc") != "$UID" ]]; then echo $psrc not owned by current user $USER echo elevating to sudo use_sudo="sudo " co+=" -s" fi if confirm do you want to move $src to $dest/$ddir and link back; then if $dr copy $co -m -i $src $dest/$ddir; then echo directory copied successfully echo moving source to temporary directory $src-tmp if $dr $use_sudo mv $src $src-tmp; then echo success, creating link back now if $dr $use_sudo ln -s "$dest/$ddir" "$src"; then if [[ ! $dr ]]; then echo success echo destination $dest/$ddir ls -la $dest/$ddir echo from link at $src ls -la $src ls -Lla $src if confirm -s Was the move and link back succesfull; then echo deleting the temporary directory $src-tmp $dr $use_sudo rm -rf $src-tmp else echo restoring $src from $src-tmp $dr $use_sudo rm $src $dr $use_sudo mv $src-tmp $src fi fi fi else echo failed to move source to temporary directory so not creating link back fi else echo rsync copy failed, aborting link back fi else echo aborting move and link back fi } # # if script was executed then call the function (return 0 2>/dev/null) || mvlnbk $@