Hardlink sub-directories and files

Dan Stromberg drsalists at gmail.com
Wed Aug 3 03:59:59 CEST 2011


On Tue, Aug 2, 2011 at 3:13 AM, Thomas Jollans <t at jollybox.de> wrote:

> On 02/08/11 11:32, loial wrote:
> > I am trying to hardlink all files in a directory structure using
> > os.link.
> >
> > However I do not think it is possible to hard link directories ?
>

That is pretty true.  I've heard of hardlinked directories on Solaris, but
that's kind of an exception to the general rule.


> > 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
> >
>
> Yes, you have to mkdir everything. However, there is an easier way:
>
> subprocess.Popen(['cp','-Rl','target','link'])
>
> This is assuming that you're only supporting Unices with a working cp
> program, but as you're using hard links, that's quite a safe bet, I
> should think.
>

A little more portable way:

$ cd from; find . -print | cpio -pdlv ../to
cpio: ./b linked to ../to/./b
../to/./b
cpio: ./a linked to ../to/./a
../to/./a
cpio: ./c linked to ../to/./c
../to/./c
../to/./d
cpio: ./d/1 linked to ../to/./d/1
../to/./d/1
cpio: ./d/2 linked to ../to/./d/2
../to/./d/2
cpio: ./d/3 linked to ../to/./d/3
../to/./d/3
0 blocks

However, you could do it without a shell command (IOW in pure python) using
os.path.walk().
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110802/1a80b9f9/attachment.html>


More information about the Python-list mailing list