LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 06-06-2006, 12:27 AM   #1
tdhanu_2000
LQ Newbie
 
Registered: May 2006
Posts: 4

Rep: Reputation: 0
Why we can't create hard links for directories in linux?


In linux, why we can not create hard links to directories?
Hard links to only files are provided?
Can anyone help?
 
Old 06-06-2006, 08:36 AM   #2
Harlin
Member
 
Registered: Dec 2004
Location: Atlanta, GA U.S.
Distribution: I play with them all :-)
Posts: 316

Rep: Reputation: 38
ln -s TARGET DIRECTORY
 
Old 06-06-2006, 08:49 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Hard-links of all types are available. Symbolic links, though, are more useful in many cases.

Quote:
Background Information...
For historical reasons, all files in Linux/Unix are called inodes. This is the physical data-structure that contains the file. Directory entries point to these inodes. (Directories are, themselves, contained in inodes.)

A hard link creates a case where two or more directory entries contain the same inode number. No additional search is required. The inode is not released until its hard-link count (normally '1') reaches zero.

A symbolic link is, in effect, a special type of file which contains a file-name or directory-name. Upon encountering a symbolic link, a search is made for the target of the symbolic link, using an ordinary search.
 
Old 06-06-2006, 11:38 PM   #4
tdhanu_2000
LQ Newbie
 
Registered: May 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks Sundialsvcs for your reply. But my question still remains unanswered. My question was "Why we can not create hard links to directories in unix?"
 
Old 06-07-2006, 05:31 AM   #5
jineshkj
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 33

Rep: Reputation: 15
directory contains other entries

suppose that you have created a hard link to a directory, the files inside the directory still are the same normal files accessed from different locations. now, if you try to remove any one of the directory using 'rm -rf' command, the files will be removed before the directory itself is removed. obviously, this is not what we want... right?


i guess it could be the reason...
 
Old 06-07-2006, 05:45 AM   #6
Harlin
Member
 
Registered: Dec 2004
Location: Atlanta, GA U.S.
Distribution: I play with them all :-)
Posts: 316

Rep: Reputation: 38
No, the targeted directories will not be removed (this has been my experience on Redhat boxes) -- only the links are removed.
 
Old 06-07-2006, 06:03 AM   #7
jineshkj
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 33

Rep: Reputation: 15
is it possible to create hard links to directories?

Quote:
Originally Posted by Harlin
No, the targeted directories will not be removed (this has been my experience on Redhat boxes) -- only the links are removed.
i tried to create hard link to directory in my fc4 box, but it says "Operation not permitted". so, how did u do this in your redhat system...
 
Old 06-07-2006, 06:18 AM   #8
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
from the GNU ln manual it seems that "you cannot make a hard link to a directory"
 
Old 06-07-2006, 06:21 AM   #9
jineshkj
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 33

Rep: Reputation: 15
then i believe i was right !!
 
Old 06-07-2006, 07:21 AM   #10
Harlin
Member
 
Registered: Dec 2004
Location: Atlanta, GA U.S.
Distribution: I play with them all :-)
Posts: 316

Rep: Reputation: 38
Perhaps our definitions are screwed up (on my end). I use hard links (or perhaps what I thought they were) for apps like WebSphere and Apache whenever I need to map out a partition that is either mounted from remote or it's just in an area on the local drive where there's a good reason to keep it there (outside of the usual locations like /var/www/html or /opt/WebSphere/AppServer/installedApps)... If I'm wrong, please let me know so I don't keep looking stupid not so much here... but if I'm at work and I'm in a meeting describing my mounts.

H
 
Old 06-07-2006, 07:39 AM   #11
jineshkj
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 33

Rep: Reputation: 15
hi,

what i believe is that we cannot create hard links to directories. and the reason i think this is not permitted has already been explained in a previous reply. i couldn't get exactly what you meant, but hope that you are using sym links and not hard links...
 
Old 06-11-2006, 04:14 AM   #12
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
[meant to be read after you read out sundialsvcs post above:]

You can make symlinks to directories, but you can't make hard links to them. Each directory has hard links --- its listing in its parent directory, its . entry, and the .. entry in each of its subdirectories --- but to impose order on the filesystem, no other hard links to directories are allowed. Consequently, the number of files in a directory is equal to the number of hard links to that directory minus two (you subtract the directory's name and the . link).

if you are still not clear:

To begin, let's review the differences between the two types of links. A symbolic link can be thought of as a "pointer", similar to a shortcut on a Windows system, that records another file or directory's location (relative or absolute). Symbolic links are like a trail of breadcrumbs leading to a new system location when files are not stored in their usual place. For example, if you ran out of space in /var and wanted to move all your messages files to /opt, then you might leave a symbolic link pointing from /var/adm to /opt/adm (i.e., /var/adm ? /opt/adm) indicating to anyone using the system that the files have been moved. A good systems administrator will always make changes such as this obvious so that no one working on the system has to work very hard determining where the files in question have been moved.

Symbolic links also provide a quick way to access a directory. For example, I might create a symbolic link called "logs" in my home directory pointing to /opt/apps/apache/logs so that I can switch to that directory with fewer keystrokes, regardless which shell I am using (aliases can be defined in some but not all shells).

A hard link also references an existing file that is, in all ways save its location in the file system, identical to the original file. It employs the same inode to keep track of the file's descriptive information and refers to the same disk space. I prefer hard links for two reasons: 1) they occupy less system resources by sharing both disk space and inode, and 2) they require fewer disk accesses (a symbolic link requires an extra read to get the name of the file about to be read).

Hard links are often used to make "copies" of files without having to allocate additional disk space and without concern that the "copy" and the original file will get out of synch. Hard links also provide additional names for executables. In fact, many system commands are hard linked. On Solaris systems, /usr/bin/adb is the same file as /usr/bin/ps. Hard linking gives an executable the opportunity to invoke a different character depending on how it is called, while still sharing a good deal of common code.

Because hard links share inodes with each other, they must exist in the same file system. Inodes are part of a file system, defined when a file system is created. This is not true of symbolic links, which can refer to a file anywhere on a system since they only store the pathname to the referenced file.

So, when can't you use a hard link? The first answer is clear from what I said above; when you are referring to a file or directory that is part of another file system, you cannot use a hard link. However, you can use a symbolic link or copy the file to the new location, which risks the files becoming out of synchronization with each other. The more surprising answer, though it makes complete sense when you think it through, is that you cannot use a hard link when the file that you're linking to is a symbolic link -- unless that symbolic link contains an absolute (not a relative) path to the file that it links to or is in the same directory as the link being created. Why? A hard link is a duplicate of a file, another reference in the file system; hard linking a symbolic link will, therefore, create another symbolic link and, if the relative location is not valid from the new link's location, then the link will be "dangling" (i.e., pointing to something which doesn't exist).

Let's take a simple example. You create a hard link to point to /usr/java/bin/java so that you can also refer to it as "myjavaapp":

% ln /usr/java/bin/java myjavaapp

What you didn't realize was /usr/java/bin/java was a symbolic link pointing to .java_wrapper. Your link will also point to .java_wrapper. Unless you just happen to have this file in your directory too (not very likely!), myjavaapp isn't going to work as you expected.

source:
http://www-rohan.sdsu.edu/doc/debian/ch-advanced.html
http://www.itworld.com/nl/unix_sys_a.../pf_index.html

Last edited by prozac; 06-11-2006 at 04:23 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
links (hard links and soft links..) sachitha Programming 1 08-10-2005 12:10 PM
Links for Directories GregSims General 5 08-06-2005 02:53 PM
Symbolic Links, directories, and relative paths (../). Can it be done? jimwillsher Linux - Newbie 1 03-11-2005 05:27 PM
Sym links and hard links akudewan Linux - Newbie 4 02-09-2005 05:08 AM
links (directories and devices) PJota Linux - Newbie 1 07-29-2003 10:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 04:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration