0

It is not allowed to create a hard link for a dir.

But the soft like will not work, if I later change the name of the dir. How would you solve the dilemma?

Are there applications which can

  • make soft links work after changing the dir's name? Or

  • make hard links work for dirs?

  • Some systems (notably HFS+ on OS X) do permit hard links to directories, however. See this Stack Overflow post for details. – Michael Homer Jul 27 '14 at 2:00
  • 1
    mount --bind does the thing you want. There are also slave, shared, and unbindable mount types. You can move mounts. You can do these things recursively as well - as in, you can mount a tree and all of its submounts elsewhere as simply as mount --rbind – mikeserv Jul 27 '14 at 4:50
  • @Tim dooes that mean the question MichaelHomer linked to answers your question? It doesn't sound like a duplicate to me, but if it answered your question then I guess it is. – derobert Jul 27 '14 at 5:25
  • it doesn't. @derobert – Tim Jul 27 '14 at 5:26
  • 1
    @derobert The question has been edited substantially since then, and it's no longer a duplicate. I retracted my close vote. – Michael Homer Jul 27 '14 at 5:45
3

As you have said, directory hard links are not possible.

Perhaps you could move the original directory to a different location, such as a hidden folder, and make both of your directories soft links to the real location. That way you could rename them freely, and the links would still be valid.

This shell alias may help:

function mkdirlink {
        directory="$HOME/.directories/$(uuidgen)"
    mkdir "$directory"
    ln -s "$directory" "$1"
}

You may also be interested in Dantalian, a command line tool for managing files using hardlinks.

  • That is very troublesome, since it is inevitable that I need to reorganize the actual directories, change their names and contents sometimes. – Tim Jul 27 '14 at 3:05
  • 1
    What is the problem? You just create a hidden directory somewhere and put your directories there, then link to them where you actually want them. You can just ignore the fact that you are dealing with links rather than actual directories. – Ian D. Scott Jul 27 '14 at 3:09
  • I need to swap files between directories, move directories to be children of other directories, create new directories, change names of directories, ...(any operations with directories). If not working with the actual directories but with soft links, I don't know how to keep my mind clear. – Tim Jul 27 '14 at 3:11
  • You just pretend they aren't links. Real directories are just links to inodes, so they are much the same, just at a lower level. Soft links act like actual directories for most purposes. – Ian D. Scott Jul 27 '14 at 3:16
  • (1) When you back up a whole partiiton, will the soft links still work in the backup? (2) when removing a dir, removing a link and the contents of the dir it links to, will it be good to leave an empty useless actual dir? – Tim Jul 27 '14 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.