Created
February 19, 2011 00:10
-
-
Save rtlong/834652 to your computer and use it in GitHub Desktop.
This script automates the process of moving a file or directory, then creating a link from it's new location to it's old location. I am using it to add application config files to my Dropbox folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Author: Ryan Long (ryan@rtlong.com) | |
# | |
# This script requires two arguments, akin to the `mv` command, and it automates the following process: | |
# - mv $1 $2 | |
# - ln -s $2 $1 | |
# | |
# More info is below in the dohelp() function; or call the script with the `--help' option. | |
# Hack prevention | |
PATH="/sbin:/usr/sbin:/bin:/usr/bin" | |
# Error codes | |
wrong_params=5 | |
interrupted=99 | |
default_error=1 | |
# Function to echo in color. Don't supply color for normal color. | |
echo_color() | |
{ | |
message="$1" | |
color="$2" | |
red_begin="\033[01;31m" | |
green_begin="\033[01;32m" | |
yellow_begin="\033[01;33m" | |
color_end="\033[00m" | |
# Set color to normal when there is no color | |
[ ! "$color" ] && color_begin="$color_end" | |
if [ "$color" == "red" ]; then | |
color_begin="$red_begin" | |
fi | |
if [ "$color" == "green" ]; then | |
color_begin="$green_begin" | |
fi | |
if [ "$color" == "yellow" ]; then | |
color_begin="$yellow_begin" | |
fi | |
echo -e "${color_begin}${message}${color_end}" | |
} | |
end() | |
{ | |
message="$1" | |
exit_status="$2" | |
if [ -z "$exit_status" ]; then | |
exit_status="0" | |
fi | |
if [ ! "$exit_status" -eq "0" ]; then | |
echo_color "$message Type --help" "red" | |
else | |
echo_color "$message" "green" | |
fi | |
# if [ "$exit_status" -eq "$wrong_params" ]; then | |
# dohelp | |
# fi | |
exit $exit_status | |
} | |
# Define function to call when SIGTERM is received | |
trap "end 'Interrupted' $interrupted" 1 2 3 15 | |
dohelp() | |
{ | |
cat <<'END-OF-HELP-MESSAGE' | |
Usage: move-then-link-to SOURCE DEST | |
or: move-then-link-to SOURCE DIRECTORY | |
Rename SOURCE to DEST, or move SOURCE to DIRECTORY. | |
This script requires two arguments, akin to the bash 'mv' command, and it automates the following process: | |
- mv SOURCE DEST | |
- ln -s DEST SOURCE | |
The script includes verification that the destination exists and should except those two arguments just like | |
'mv' would. That is, the following are equivalent: | |
~$ move-then-link-to .filezilla Dropbox/.config/.filezilla | |
~$ move-then-link-to .filezilla Dropbox/.config/ | |
Both will move the .filezilla directory to Dropbox/.config/.filezilla and then create a symbolic | |
link ~/.filezilla that points to /home/[username]/Dropbox/.config/.filezilla | |
As you may have noticed. I wrote this script to move application config files into my Dropbox | |
directory for synchronization with my other computers. To set those computers up to sync these | |
files, I will just create the link manually, and ignore any local files that mmy already exist. | |
END-OF-HELP-MESSAGE | |
# Exit because you don't want the script to do anything after displaying help | |
exit | |
} | |
if [ -z "$*" ]; then | |
dohelp | |
fi | |
SOURCE=$1 | |
DEST=$2 | |
shift 2 | |
while [ -n "$*" ]; do | |
flag=$1 | |
value=$2 | |
case "$flag" in | |
"--help") | |
dohelp ;; | |
"--") | |
break ;; | |
*) | |
end "unknown option $flag." "$wrong_params" ;; | |
esac | |
shift | |
done | |
if [ -z "$SOURCE" ]; then | |
end "SOURCE not given." $wrong_params | |
fi | |
if [ -z "$DEST" ]; then | |
end "DEST or DIRECTORY not given." $wrong_params | |
fi | |
if [ ! -e "$SOURCE" ]; then | |
end "$SOURCE does not exist." $wrong_params | |
fi | |
if [ -f "$DEST" ]; then | |
end "$DEST exists as a file." $wrong_params | |
fi | |
if [ -d "$DEST" ]; then | |
# DEST is a directory. Set the LINK_TARGET var to what wv will assume | |
TARGET_DIRECTORY="$DEST" | |
LINK_TARGET="$TARGET_DIRECTORY"/`basename "$SOURCE"` | |
else | |
# DEST is not a directory. check if it's parent is. | |
TARGET_DIRECTORY=`dirname "$DEST"` | |
if [ ! -d $TARGET_DIRECTORY ]; then | |
# Parent isn't either. | |
end "Neither $DEST nor $TARGET_DIRECTORY is a directory." $wrong_params | |
fi | |
# Parent is a directory; $DEST is the LINK_TARGET | |
LINK_TARGET="$DEST" | |
fi | |
# canonicalize TARGET_DIRECTORY and LINK_TARGET into absolute paths | |
TARGET_DIRECTORY=`readlink -e "$TARGET_DIRECTORY"` | |
LINK_TARGET=`readlink -f "$LINK_TARGET"` | |
SOURCE=`readlink -e "$SOURCE"` | |
#echo "All good so far!! :-] Would be linking $LINK_TARGET to $SOURCE." | |
echo mv --verbose --interactive "$SOURCE" "$LINK_TARGET" | |
mv --verbose --interactive "$SOURCE" "$LINK_TARGET" | |
echo ln --verbose --interactive --symbolic --no-target-directory "$LINK_TARGET" "$SOURCE" | |
ln --verbose --interactive --symbolic --no-target-directory "$LINK_TARGET" "$SOURCE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment