home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 18.4 More About Links Chapter 18
Linking, Renaming, and Copying Files
Next: 18.6 Stale Symbolic Links
 

18.5 Creating and Removing Links

The ln command creates both hard and soft (symbolic) links (18.4 ) . Only UNIX versions with symbolic links have the -s option, though:

% ln

 filename linkname

   To create a hard link

% ln -s

 filename linkname

   To create a symbolic link

In either case, filename must already exist. Otherwise you will get an error message. If you are using Berkeley UNIX, linkname must not exist - if it does, you will get an error. Under System V, linkname may already exist; if you are allowed to write the file, ln destroys its old contents and creates your link. If you don't have write access for linkname , ln asks whether or not it is OK to override the file's protection. For example:

% ln foo bar


ln: override protection 444 for bar? y

Typing y gives ln permission to destroy the file bar and create the link. Note that this will still fail if you don't have write access to the directory.

You are allowed to omit the linkname argument from the ln command. In this case, ln takes the last component of filename (i.e., everything after the last slash) and uses it for linkname . Of course, this assumes that filename doesn't refer to the current directory. If it does, the command will fail: the link will already exist. For example, the commands below are the same:

&..
 
% ln -s ../archive/file.c


% ln -s ../archive/file.c file.c

Both create a link from file.c in the current directory to ../archive/file.c .

ln also lets you create a group of links with one command, provided that all of the links are in the same directory. Here's how:

% ln

 file1 file2 file3 ... filen directory

This command uses the filename from each pathname (after the last slash) as each link's name. It then creates all the links within the given directory . For example, the first command below is equivalent to the next two:

% ln ../s/f1 ../s/f2 current


% ln ../s/f1 current/f1


% ln ../s/f2 current/f2

You can replace this list of files with a wildcard expression (15.2 ) , as in:

&.
 
% ln -s ../newversion/*.[ch] 

Note that symbolic links can get out-of-date (18.6 ) . [Hard links can also be "broken" in some situations. For example, a text editor might rename the link textfile to textfile.bak , then create a new textfile during editing. Previous links to textfile will now give you textfile.bak . To track down this problem, find the links (17.22 ) to each file. -JP  ]

To remove a link, either hard or symbolic, use the rm command.

- ML


Previous: 18.4 More About Links UNIX Power Tools Next: 18.6 Stale Symbolic Links
18.4 More About Links Book Index 18.6 Stale Symbolic Links

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System










??????????????@Mail.ru