Skip to content

Instantly share code, notes, and snippets.

@hatsuyuki15
Forked from xombiemp/lndir.sh
Created July 9, 2022 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hatsuyuki15/397fe02d5dc394924b8ab35fa23a1b20 to your computer and use it in GitHub Desktop.
Save hatsuyuki15/397fe02d5dc394924b8ab35fa23a1b20 to your computer and use it in GitHub Desktop.
This script will allow you to effectively hard link a directory. It reproduces the directory structure of the source in the destination and then recursively hard links all the files from the source to the corresponding location in the destination. If you've ever wanted to hard link a folder, this produces the results you want.
#!/bin/bash
oldifs=$IFS
IFS='
'
[ $# -ne 2 ] && { echo "Usage: $0 sourceDirectory destinationDirectory" ; exit 1; }
[ ! -d "$1" ] && { echo "$1 is not a valid directory"; exit 1; }
[ ! -d "$2" ] && { mkdir -p "$2"; }
src=$(cd "$1" ; pwd)
dst=$(cd "$2" ; pwd)
find "$src" -type d |
while read dir; do
mkdir -p "$dst${dir#$src}"
done
find "$src" -type f -o -type l |
while read src_f; do
dst_f="$dst${src_f#$src}"
ln "$src_f" "$dst_f"
done
IFS=$oldifs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment