[news.newusers.questions] ln question

cwilson@NISC.SRI.COM (Chan Wilson) (02/11/90)

question:  Does 'ln'ing a file make multiple copies?  

Reason I ask is thus: After I ln a file, then do a ls on it, it doesn't 
show that it's a linked file (unless I do ln -s).  Furthermore, it appears
to be taking up the same amount of space.

What's the right answer?

--Chan
			   ................
  Chan Wilson -- cwilson@nisc.sri.com <or> radius!cwilson@apple.com
Janitor/Architect of comp.binaries.apple2 archive on wuarchive.wustl.edu
	      I don't speak for SRI, someone else does.
			   ................

eps@toaster.SFSU.EDU (Eric P. Scott) (02/12/90)

In article <13085@fs2.NISC.SRI.COM> cwilson@NISC.SRI.COM (Chan Wilson) writes:
>question:  Does 'ln'ing a file make multiple copies?

Assuming you're running UNIX (and not some "imitation" UNIX), the
answer is no, there are no multiple copies.

Each UNIX file is held by an inode (pronounced eye-node).  The inode
points to actual storage locations for disk files, and contains nearly
all the other interesting properties--size, dates, ownership,
permissions, etc.  Inodes do *not* contain file names--file names
only exist in directories.

A directory is a collection of name:inode-number pairs.  Making a
"hard" link involves creating an additional directory entry
pointing to the same inode.  The inode contains a "link count"
which is the number of directory entries that point to it.  A
file with no links and no references goes away (that's why UNIX
has no file "delete"--only "unlink").  An inode doesn't know
which directories point to it, only how many.

Hard links are indistinguishable from each other (you can't tell
which came first)--they point to the same inode, and hence the
same file.  ls -l  will normally show you the link count; the -i
option will show inode numbers as well.  Two directory entries on
the same filesystem with the same inode number reference the same
file.  (Exercise for the reader: why do most files have one link,
but newly created directories two?)

Some UNIX implementations implement "symbolic" or "soft" links;
each consumes its own inode to hold a file name, but no "data"
as we normally think of it.  Again, no copy is made of the
linked-to file (if it even exists!).

The usual "backup" utilities attempt to keep track of multiple
links.  If entire filesystems are backed up, there shouldn't be
any problems.  If backups are piecemeal, a file that spans
savesets could potentially be restored as separate files, and
in that case a copy would be created and the links broken.
"There's nothing wrong with your system, just your administrator."

					-=EPS=-

bill@twwells.com (T. William Wells) (02/12/90)

In article <13085@fs2.NISC.SRI.COM> cwilson@NISC.SRI.COM (Chan Wilson) writes:
: question:  Does 'ln'ing a file make multiple copies?
:
: Reason I ask is thus: After I ln a file, then do a ls on it, it doesn't
: show that it's a linked file (unless I do ln -s).  Furthermore, it appears
: to be taking up the same amount of space.
:
: What's the right answer?

Linking merely makes an entry in a directory; no new copies are
created. Do an ls -l and look at the second column of the
listing, the link count to see that something has happened when
you do a link.

This kind of question should be asked of someone local, rather
than of the whole net. However, if you can't find someone local
to answer your questions, try the newsgroup comp.unix.questions.

---
Bill                    { uunet | novavax | ankh } !twwells!bill
bill@twwells.com