Skip to content

Instantly share code, notes, and snippets.

@xombiemp
Last active December 1, 2025 06:33
Show Gist options
  • Select an option

  • Save xombiemp/4748562 to your computer and use it in GitHub Desktop.

Select an option

Save xombiemp/4748562 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
@wenzelie
Copy link

As of reading this, I'm under the impression that this does the same thing as:
cp -lR
Please correct me if I'm wrong.

@chandeeland
Copy link

@wenzelie hard links do not require the space. it creates a inode pointer to the same data.

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