Recursively creating hardlinks using python
You just have to call os.system("cp -Rl dir1 dir2")
, no need hand write your own function.
Edited: Since you want do this in python.
You are right: It's available in module shutil
:
shutil.copytree(src, dst, copy_function=os.link)
Here's a pure python hardcopy function. Should work the same as cp -Rl src dst
import os
from os.path import join, abspath
def hardcopy(src, dst):
working_dir = os.getcwd()
dest = abspath(dst)
os.mkdir(dst)
os.chdir(src)
for root, dirs, files in os.walk('.'):
curdest = join(dst, root)
for d in dirs:
os.mkdir(join(curdst, d))
for f in files:
fromfile = join(root, f)
to = join(curdst, f)
os.link(fromfile, to)
os.chdir(working_dir)