#!/usr/bin/env bash declare -a sources target=$1; shift; i=0 while [ "$#" -gt 0 ]; do sources[$i]="$1"; shift; let i=i+1; done function move-and-link-to-folder { local s="$1"; shift; local target="$1"; shift; local name=$(basename "$s"); local sdir=$(dirname "$s"); local target_path=$(realpath -Ls --relative-to="$sdir" "$target/$name" ) mv "$s" "$target" ln -sf "$target_path" "$s" } function move-and-link { local s="$1"; shift; local target="$1"; shift; local sdir=$(dirname "$s"); local target_path=$(realpath -Ls --relative-to="$sdir" "$target" ) mv "$s" "$target" ln -sf "$target_path" "$s" } if [ "$i" -gt 1 ]; then if [ -d "$target" ]; then for s in "${sources[@]}"; do move-and-link-to-folder "$s" "$target" done else echo "Target $target is not directory." exit 1 fi else s="${sources[0]}" if [ -d "$target" ]; then move-and-link-to-folder "$s" "$target" else move-and-link "$s" "$target" fi fi