[Courses] [FS] Symbolic links, the amazing disappearing laptop, and other spectacles

meredydd at lazzurs.net meredydd at lazzurs.net
Wed Jul 16 15:52:50 EST 2003


Many apologies, everyone. At the moment, I'm typing this on a webmail
interface, on a bitterly-contested family computer, so I can't be long.

In any case - I think those symbolic links need clearing up a bit. Some of
the posts here are completely accurate, and some are (unintentionally) a
bit misleading. I'm sorry about this, but I really can't explain now. I'll
be getting my laptop sometime today or tomorrow, just hang on please!

As for the directories - this I *can* deal with here. The actual
filesystem entity you refer to with the path "/usr" is the same as the
entity you refer to with "/usr/". It's just that the first of those two
naming conventions doesn't specify whether it's a file called "usr" in the
directory "/", or a directory. The second makes it abundantly clear - a
trailing slash means a directory. Hence, I use the trailing slash whenever
I talk about directories.

This is, in fact, the cause of the behavioural differences with certain
commands. Many commands, such as "cp", "mv" and even "ln", can operate in
two modes. The first is common sense - they take two arguments. For
example:

cp file1 file2
   ...makes a copy of the file "file1" called "file2"

mv file1 file2
   ...renames "file1" to "file2"

ln -s file1 file2
   ...creates a symbolic link to "file1", called "file2"

However, there's also a second mode:

cp file1 file2 ... fileX dest_dir/
   ...makes a copy of each of file1 ... fileX, with the same name in each
case, and puts them in the directory dest_dir/

mv file1 file2 ... fileX dest_dir/
   ...moves file1 ... fileX into the directory dest_dir/

ln -s file1 file2 ... fileX dest_dir/
   ...makes a symlink to each of file1 ... fileX in the directory
dest_dir. Note that because symbolic links contain relative paths, if
the filenames aren't absolute paths, then you will end up with a link
in dest_dir/ called "file1", pointing to "file1" - that is, itself, and
so on. Not good.


In any case, all of these programs have to decide for themselves which
form of the command you are using! This makes things difficult in
ambiguous cases. For example, take this command:

cp path1 path2

Oh, great, now we're stuck. How is "cp" supposed to tell which form of the
command we're using? In fact, what it does is to look at path2 - ask the
operating system what it is.

If it's a directory, then we're using form 2 - just copy path1 into path2/.

If it's a file, then we're using form 1 of the command - overwrite path2
with a copy of path1 (unless you passed it the -i option, in which case
ask you first. -i is a good thing, it has saved my bacon on numerous
occasions. Use it.).

And finally, we get back to the trailing slash. If we repeat the above
command, but make sure that path2 has a slash on the end of it, "cp" will
*know* we're talking about a directory here. And if it's not, then the
operating system will complain ("path2: not a directory") when "cp" tries
to do directory-type things to path2, like list its contents.

Hmm...not exactly sure how that explanation qualifies as either "short" or
"quick", but my little sister wants the computer so I think I'll leave it
at:

HTH, HAND

Meredydd



More information about the Courses mailing list