Skip to content

Instantly share code, notes, and snippets.

@nyxkrage
Created June 18, 2025 15:50
Show Gist options
  • Save nyxkrage/ab8989a6977f450f3895056fc9c99d5b to your computer and use it in GitHub Desktop.
Save nyxkrage/ab8989a6977f450f3895056fc9c99d5b to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
if [ -z "$1" ]; then
echo "Usage: $0 <path>"
exit 1
fi
SYMLINK="$1"
TARGET=""
TEMP_DIR=""
BASENAME=""
PARENT_DIR=""
SYMLINK_REMOVED=false
cleanup() {
if [ "$SYMLINK_REMOVED" = true ]; then
if [ -e "$PARENT_DIR/$BASENAME" ]; then
rm -rf "$PARENT_DIR/$BASENAME"
fi
if [ ! -L "$SYMLINK" ]; then
ln -s "$TARGET" "$SYMLINK"
fi
fi
if [ -d "$TEMP_DIR" ]; then
rm -rf "$TEMP_DIR"
echo "Removed temporary directory."
fi
}
trap cleanup EXIT INT TERM
if [ ! -L "$SYMLINK" ]; then
echo "Error: '$SYMLINK' is not a symbolic link."
exit 1
fi
TARGET=$(readlink -f "$SYMLINK")
TEMP_DIR=$(mktemp -d)
BASENAME=$(basename "$TARGET")
PARENT_DIR=$(dirname "$SYMLINK")
cp -a "$TARGET" "$TEMP_DIR/"
unlink "$SYMLINK"
SYMLINK_REMOVED=true
cp -a "$TEMP_DIR/$BASENAME" "$PARENT_DIR/"
echo "------------------------------------------------------------------"
echo "You are in a temporary shell. $SYMLINK is an ephemeral copy of its original target."
echo "Type 'exit' to close this shell and restore the original symlink."
echo "------------------------------------------------------------------"
bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment