UNIX For Dummies [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

UNIX For Dummies [Electronic resources] - نسخه متنی

John Levine, Margaret Levine Young

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






A File by Any Other Name

Sometimes, having a file in more than one place is nice (that budget file we keep mentioning, for example). If you are working on it with someone else, the file can be in both your home directory and your coworker’s home directory so that neither of you have to use the cd command to get to it.
A nice feature of UNIX (and you thought there weren’t any!) is that this situation is possible — even easy to set up. A single file can have more than one name, and the names can be in different directories.

Suppose that two authors are working on a book together (a totally hypothetical example). The chapters of the book are in John’s directory:

/usr/johnl/ book . What about Margy? Having to type the following line every time work on the book begins is annoying:

cd /usr/johnl/book
Instead, the files could also be in /usr/margy/book .


How can you be in two places at once when you’re not anywhere at all?


The way to let a file be in two places at once is with the ln (for link) command. You tell ln two things:



The current name of the file or files you want to create links to

The new name

Start with just one file. Margy wants to make a link to the chapterlog file (it contains the list of chapters). The file is in /usr/johnl/book . In her book directory, Margy types this line:


ln /usr/johnl/book/chapterlog booklog

UNIX says absolutely nothing; it just displays another prompt. (No news is good news.) It just created a link, or new name, however, to the existing chapterlog file. The file now appears also in /usr/margy/book as booklog . You have only one file (UNIX doesn’t make a copy of the file or anything tacky like that) with two names.


Playing the links


After you create a link by using ln , the file has two names in two directories. The names are equally valid. It isn’t as though /usr/johnl/book/chapterlog is the real name, and /usr/margy/book/booklog is an alias. UNIX considers both names to be equally important links to the file.


Deleting links


To delete a link, you use the same rm command you use to delete a file. In fact, rm always just deletes a link. It just so happens that, when no links to a file exist, the file dries up and blows away. When you use rm on a file that has just one name (link), the file is deleted. When you use rm on a file that has more than one name (link), the command deletes the specified link (name), and the file remains unchanged, along with any other links it may have had.


Renaming a link


You can use the old mv command to rename a link, too. If Margy decides that for the book-status file to have the same name in both places is less confusing (as it stands now, it’s chapterlog to John and booklog to Margy), she can type this line:


mv booklog chapterlog

You can even use the mv command to move the file to another directory.


Linking a bunch of files


You can also use ln to link a bunch of files at the same time. In this case, you tell ln two things:



The bunch of files you want to link, probably using a wildcard character such as chapter* . You also can type a series of filenames or a combination of names and patterns. (UNIX may be obscure, but it’s flexible.)

The name of the directory in which you want to put all the new links.



The ln command uses the same names the files currently have when it makes the new links. It just puts them in a different directory.

The chapterlog business in the preceding example, for example, works so well that Margy decides to link to all the files in /usr/johnl/book . To make links in /usr/margy/book , she types this line:


ln /usr/johnl/book/* /usr/margy/book









Tip Linking once and linking twice


Here’s one caveat. The ln command in the example in this section links all the files that exist at the time the command is given. If you add new files to either /usr/margy/book or /usr/johnl/book , the new files are not automatically linked to the other directory. To fix this situation, you can type the same ln command every few days (or whatever frequency makes sense). The command tells you that lots of files are already identical in the two directories and makes links for the new files.

If you linked to someone else’s files, you may have permission to read those files but not to change or write to them. When you ask ln to make the new links, if it tries to replace a file you can’t write to, it says something like this:

ln: chapter13: 644 mode?


See Chapter 25 for the exact meaning of this uniquely obscure message. Press the y key if you want to replace the file, which you probably do in this case. Press the n key if you don’t want to replace the file.











This command tells UNIX to create links for all the files in /usr/johnl/book and to put the new links in /usr/margy/book . Now every file that exists in /usr/johnl/book also exists in /usr/margy/book . Margy uses the ls command to look at a file listing for her new book directory. It contains all the book files. This arrangement makes working on the files much more convenient.


Linking across the great computer divide


Network All this talk about links assumes that the files you’re linking to are on the same file system (that’s UNIX-speak for disk or disk partition). If your computer has several hard drives or if you’re on a network and use files on other computers (through NFS or some other system, as explained in Chapter 16), some of the files you work with may be on different file systems.

Here’s the bad news: The original ln command couldn’t create links to files on other file systems. Bummer. But all modern UNIX systems have things called soft links, or symbolic links (symlinks, for short) that are almost as good.

Soft links enable you to use two or more different names for the same file. Unlike regular links (or hard links), however, soft links are just imitation links. UNIX doesn’t consider them to be the file’s real name.


Making soft links


Linux To make a soft link, add the -s option to the ln command.

Suppose that you want a link in your home directory to the recipe.list file in /usr/gita . In your home directory, you type this line:


ln /usr/gita/recipe.list gitas.recipes

Rather than respond with serene silence, UNIX responds with this line:

ln: different file system
Drat! Gita’s home directory is on a different file system from yours, perhaps even on a different computer. So you make a soft link by sticking an -s into the command:


ln -s /usr/gita/recipe.list gitas.recipes

As usual, no news is good news;

ln says nothing if it works. Now a file called gitas.recipes seems to be in your home directory — all through the magic of soft links. You still have only one file, but you have an extra link to it.


Using soft links


You can look at, copy, print, and rename a soft-linked file as usual. If you have the proper permissions, you can edit it. If Gita deletes her file, though, the file vanishes. Your soft link now links to an empty hole rather than to a file, and you see an error message if you try to use the file. UNIX knows that the soft link isn’t the file’s real name. When you see a soft link in a long ls listing, UNIX gives the name of the soft link and also the name of the file it refers to.

Tip If you try to use a file and UNIX says that it isn’t there, check to see whether it’s a dangling soft link (a link to a nonexistent file). Type ls -l to see whether the file is a soft link. If it is, use another ls -l on the real filename to make sure that the file really exists.

To get rid of a dangling soft link, use the rm command to delete it.

/ 213