os.link makes a copy, not a link

D

Dan M

I'm a little bit confused. According to the sources I've looked at on the
net,
os.link('file1', 'file2')
should make a hard link from file1 to file2. But what I'm finding is that
it's actually making a copy. Am I forgetting a step or something?

Python 2.3.4 running on CentOS 4.3
 
C

Carl Banks

Dan said:
I'm a little bit confused. According to the sources I've looked at on the
net,
os.link('file1', 'file2')
should make a hard link from file1 to file2. But what I'm finding is that
it's actually making a copy. Am I forgetting a step or something?

Python 2.3.4 running on CentOS 4.3

Are file1 and file2 on the same filesystem? Looks like os.link just
calls the OS's link system call, which, for your system, might copy the
file.

Carl Banks
 
B

Ben Finney

Dan M said:
I'm a little bit confused. According to the sources I've looked at on the
net,
os.link('file1', 'file2')
should make a hard link from file1 to file2. But what I'm finding is that
it's actually making a copy. Am I forgetting a step or something?

Are you sure that's what it's doing? Do you know the difference
between a copy and a hard link?

$ echo "Lorem ipsum" > foo.txt
$ ln foo.txt bar.txt
$ cp foo.txt baz.txt
$ ls -l *.txt
-rw-r--r-- 2 bignose bignose 12 2006-06-10 10:16 foo.txt
-rw-r--r-- 2 bignose bignose 12 2006-06-10 10:16 bar.txt
-rw-r--r-- 1 bignose bignose 12 2006-06-10 10:17 baz.txt
$ cat foo.txt
Lorem ipsum
$ cat bar.txt
Lorem ipsum
$ cat baz.txt
Lorem ipsum
 
N

Nick Craig-Wood

Dan M said:
I'm a little bit confused. According to the sources I've looked at on the
net,
os.link('file1', 'file2')
should make a hard link from file1 to file2. But what I'm finding is that
it's actually making a copy. Am I forgetting a step or something?

Python 2.3.4 running on CentOS 4.3

It works here (Py 2.4.3 on Ubuntu Dapper Drake)

The way to check whether you are getting a copy or a hardlink is to
see whether the inode number is the same.

Otherwise it is impossible to tell whether you have a copy or a
hardlink.
1685186 -rw-r--r-- 2 ncw ncw 4 2006-06-10 08:31 z
1685186 -rw-r--r-- 2 ncw ncw 4 2006-06-10 08:31 z2
 
N

Nick Craig-Wood

Carl Banks said:
Are file1 and file2 on the same filesystem? Looks like os.link just
calls the OS's link system call, which, for your system, might copy the
file.

The link(2) system call on linux never does that. Eg
Traceback (most recent call last):
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,829
Latest member
PIXThurman

Latest Threads

Top