Hardlink sub-directories and files

Peter Otten __peter__ at web.de
Tue Aug 2 12:01:19 CEST 2011

loial wrote:

> I am trying to hardlink all files in a directory structure using
> os.link.
> This works fine for files, but the directory also contains sub-
> directories (which themselves contain files and sub-directories).
> However I do not think it is possible to hard link directories ?
> So presumably I would need to do a mkdir for each sub-directory
> encountered?
> Or is there an easier way to hardlink everything in a directory
> structure?.
> The requirement is for hard links, not symbolic links

You cannot make hardlinks for directories, that's a restriction imposed by 
the operating system.

Look for shutil.copytree() for a template of what you are trying to do; you 
might even monkepatch it

# untested
copy2 = shutil.copy2
shutil.copy2 = os.link
    shutil.copytree(source, dest)
    shutil.copy2 = copy2

if you are OK with a quick-and-dirty solution.

More information about the Python-list mailing list