Skip to content

Instantly share code, notes, and snippets.

@gartisk
Last active February 17, 2024 21:01
Show Gist options
  • Save gartisk/69a50964c458916c32ba9cb168f43005 to your computer and use it in GitHub Desktop.
Save gartisk/69a50964c458916c32ba9cb168f43005 to your computer and use it in GitHub Desktop.
#!/bin/bash
####################################################################################
## ##
## Directory Replacer ##
## ##
## Description: Initially made with the intention of moving ##
## Umbrel apps to an external drive. ##
## ##
## Usage: ./dir-replacer.sh folder1 folder2 ... dest ##
## ##
## Authors: Guilherme Araujo aka Gartisk ##
## https://github.com/gartisk ##
## ##
## Version: 0.1 ##
## Last Update: February 17, 2023 ##
## ##
## Github easy alternative: ##
## https://github.com/new/import ##
## ##
####################################################################################
howto="Usage: $0 folder1 folder2 ... destination"
# Check if at least two arguments are provided
if [ "$#" -lt 2 ]; then
echo "$howto"
exit 1
fi
# Get the last argument, which is the destination folder
while [[ $# -gt 1 ]]; do
case $1 in
-d)
dest="$2"
shift 2
;;
*)
original_folders+=("$1")
shift
;;
esac
done
# Get the complete path of the destination folder
# Check if the destination directory is provided
if [ -z "$dest" ]; then
echo "Destination directory is required."
echo "$howto"
exit 1
fi
dest=$(realpath "$dest")
# Check if the destination folder exists
if [ ! -d "$dest" ]; then
echo "Destination folder '$dest' does not exist."
echo "Creating '$dest' folder."
cmd_mkdir="mkdir -p \"$dest\""
echo "$cmd_mkdir"
eval "${cmd_mkdir}"
fi
#original_folders=("${@:1:$#-1}")
# Move each folder to the destination folder
for ((i = 0; i < ${#original_folders[@]}; i++)); do
origem_folder="${original_folders[i]}"
echo "Move and Link Directory $origem_folder"
if [ ! -d "$origem_folder" ]; then
echo "Folder '$origem_folder' not found"
continue
fi
origem_basename=("$(basename "$origem_folder")")
origem_newpath="$dest/$origem_basename"
origem_realname=("$(realpath "$origem_basename")")
cmd_move="mv \"$origem_realname\" \"$origem_newpath\""
echo "$cmd_move"
eval "${cmd_move}"
cmd_link="ln -s \"$origem_newpath\" \"$origem_realname\""
echo "$cmd_link"
eval "${cmd_link}"
echo ""
done
echo "Done"
@gartisk
Copy link
Author

gartisk commented Feb 17, 2024

To Execute Directly:
curl -sSL https://gist.githubusercontent.com/gartisk/69a50964c458916c32ba9cb168f43005/raw/48f197c3455a9586d630047245eb874a16c76d22/dir-replacer.sh | bash -s folder1 folder2 ... -d dest
or
wget -qO- https://gist.githubusercontent.com/gartisk/69a50964c458916c32ba9cb168f43005/raw/48f197c3455a9586d630047245eb874a16c76d22/dir-replacer.sh | bash -s folder1 folder2 ... -d dest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment