Rename file without overwriting existing files

Chris Angelico rosuav at gmail.com
Mon Jan 30 09:18:39 EST 2017


On Tue, Jan 31, 2017 at 12:58 AM, Peter Otten <__peter__ at web.de> wrote:
>> I looked at help(os.link) on Python
>> 3.4 and the corresponding current library documentation on the web. I
>> saw no mention of what happens when dst exists already.
>>
>> Also, creating a hard link doesn't seem to work between different file
>> systems, which may well be relevant to Steve's case.
>
> In his example above he operates inside a single directory. Can one
> directory spread across multiple file systems?

Yep. Try unionfs.

... make ourselves some scratch space ...
$ mkdir space modifier
$ dd if=/dev/zero of=space.img bs=4096 count=65536
$ mkfs space.img
$ sudo mount space.img space
$ dd if=/dev/zero of=modifier.img bs=4096 count=1024\
$ mkfs modifier.img
$ sudo mount modifier.img modifier

... put some content into the base directory ...
$ sudo -e space/demo.txt

... and now the magic:
$ unionfs modifier=RW:space=RO joiner/
$ cd joiner

At this point, you're in a directory that is the union of the two
directories. One of them is read-only, the other is read/write. It is
thus possible to view a file that you can't hard-link to a new name,
because the new name would have to be created in the 'modifier' file
system, but the old file exists on the 'space' one.

ChrisA



More information about the Python-list mailing list