linkdir (828B)
1 #!/usr/bin/env bash 2 if [ $# -eq 0 ]; then 3 echo "Needs at least one argument (the source directory)." 4 echo "Usage: linkdir source_directory [target_directory]" 5 exit 1 6 fi 7 if [[ "$1" != */ ]]; then 8 source_dir="${1}/" 9 else 10 source_dir="$1" 11 fi 12 13 target_dir="./" 14 15 if [ $# -ge 2 ]; then 16 if [[ "$2" != */ ]]; then 17 target_dir="${2}/" 18 else 19 target_dir="$2" 20 fi 21 fi 22 23 echo "Linking content of $source_dir to $target_dir..." 24 25 cr=0 26 ca=0 27 28 for f in "${source_dir}"*; do 29 if [ -L "${target_dir}${f##*/}" ]; then 30 rm "${target_dir}${f##*/}" 31 ((cr++)) 32 fi 33 34 ln -s "$f" "${target_dir}${f##*/}" 35 echo "'$f' -> '${target_dir}${f##*/}'" 36 ((ca++)) 37 done 38 39 echo "Removed $cr local symlinks with originals in $source_dir." 40 echo "Linked $ca directories into $target_dir." 41 unset cr ca source_dir target_dir