You are not logged in.

#1 2014-01-23 00:26:22

nullAtman
Member
Registered: 2014-01-23
Posts: 4

Creating Hard Links for Directories

Hey,

(forgive me if this is not the right place to post this. It was a tricky one to place.)

I have a really big music collection and some of it I don't even really care about. So I've decided to downsize, however I still want to keep all the music and artists I've amassed.

So my solution was to:

$ mv library .complete

then

$ sudo ln library/artist -t .complete/

That way I'd have the same directory structure under library as in .complete. Expect of course I'd only cherry pick handful of files from .complete to include in library. From there I could easily drop the library directory to an mp3 player or wherever.

But of course, I got the error:

ln: ‘link’: hard link not allowed for directory

Is it possible to do something like this? I know of "mount -bind" and use it quite a bit for sticking stuff in my home directory but I don't want to drop a 50-100 mount commands in my fstab.

If you have any ideas or solutions for what I am trying to accomplish please share. It would be really nice for me to have smile

Peace!

Offline

#2 2014-01-23 02:01:56

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,678

Re: Creating Hard Links for Directories

We may move this thread.  Let's see how it goes.

Is there some reason that a symbolic link will not work?  Hard links will work between individual files, not directories.  If you have two files that are hard linked, you can delete one or the other without impacting the other files that are linked.  Only when the last link is removed is the file actually removed.   This does not work for directories (on Linux)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2014-01-23 03:51:01

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,217
Website

Re: Creating Hard Links for Directories

A symbolic link should work; just make sure when you're dumping to your mp3 player or whatever that you dereference the symlinks (--dereference with cp or --copy-links with rsync for example)

Offline

#4 2014-01-23 04:49:26

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: Creating Hard Links for Directories

The only reason that a symlink might not work is if the symlink leads to an area that is outside of a chroot'ed service (like sftp for example).  But in that case a bind mount would work just fine.  I also don't think that this case is actually one of those instances either.

If you are worried about the extra overhead of a billion bind mounts in the fstab, you can give it the 'noauto,x-systemd.automount' mount options so that it isn't actually mounted until first access.

Offline

#5 2014-01-23 13:56:17

ams1
Member
Registered: 2013-12-02
Posts: 26

Re: Creating Hard Links for Directories

If soft links don't work for you, just try copying the directory with cp --link so the directories get recreated, but the files not.

I think that I would probably just move the whole folder, and then change ".complete" to ".other", for simplicity.

Offline

#6 2014-01-29 22:44:57

nullAtman
Member
Registered: 2014-01-23
Posts: 4

Re: Creating Hard Links for Directories

Hey,

Sorry I disappeared for awhile. Thanks for your replies. So I guess using hard links on directories isn't feasible unless I hack the linux kernel or something smile Thats a bit annoying cause the way I originally envisioned makes more logical sense than using symbolic links imo.

I'm still not quite sure how I will do this. One annoying thing about symbolic link is that if I add a link to a artist folder in my library folder when you follow that link you, obviously, change where you are in the filesystem. So "cd .." won't take you back to the library folder but the complete folder. Ahh! Damn you man page for luring me in with the prospect of hard links but then refusing to allow it for directories!

One quick question I want to ask you guys. A trivial problem but driving me insane.

If I type

$ ln -s foo/bar bar

I get a working bar symlink to foo/bar. If I put in a path.

$ ln -s foo/bar new/bar

I still get a file generated called bar in new however it doesn't seem to work. Anybody know what the trick is?

Offline

#7 2014-01-29 22:58:25

jpgg
Member
Registered: 2014-01-15
Posts: 43

Re: Creating Hard Links for Directories

nullAtman wrote:

So "cd .." won't take you back to the library folder but the complete folder.

I am not sure I understand what you meant. But if you are in /home/user and follow the link /home/user/link and do 'cd ..', you will be back in /home/user.

If I put in a path.

$ ln -s foo/bar new/bar

I still get a file generated called bar in new however it doesn't seem to work. Anybody know what the trick is?

I get an error when I try this command. You should first create the 'new' directory, then create the symlink. Or did you try to achieve another result?

Last edited by jpgg (2014-01-29 23:00:10)

Offline

#8 2014-01-30 09:21:43

frostschutz
Member
Registered: 2013-11-15
Posts: 1,382

Re: Creating Hard Links for Directories

nullAtman wrote:

If I type

$ ln -s foo/bar bar

I get a working bar symlink to foo/bar. If I put in a path.

$ ln -s foo/bar new/bar

I still get a file generated called bar in new however it doesn't seem to work. Anybody know what the trick is?

ln -s does not resolve relative locations for you.

Think of it more of an 'echo foo/bar > new/bar'. If you cd new and cat bar it will still say 'foo/bar'. Thus the bar symlink will link to foo/bar which is new/foo/bar.

If you want new/bar to go to ../foo/bar, you have to write it this way.

ln -s ../foo/bar new/bar

One of my favorite symlinks is one that points to the same dir, i.e. .

ln -s . /boot/boot

regardless which directory you are in, /boot/boot links to /boot.

That way you don't get trouble in your grub.cfg when you add a superfluous /boot/ to a file path on the boot partition. A boot partition does not usually have a /boot dir but with the symlink it's there. wink

Last edited by frostschutz (2014-01-30 09:22:36)

Offline

#9 2014-01-30 19:34:53

nullAtman
Member
Registered: 2014-01-23
Posts: 4

Re: Creating Hard Links for Directories

frostschutz wrote:

ln -s does not resolve relative locations for you.

Think of it more of an 'echo foo/bar > new/bar'. If you cd new and cat bar it will still say 'foo/bar'. Thus the bar symlink will link to foo/bar which is new/foo/bar.

If you want new/bar to go to ../foo/bar, you have to write it this way.

Ahh thats makes sense. Good explanation smile The ln command isn't the most intuitive thing ever devised imo.

jpgg wrote:

I am not sure I understand what you meant. But if you are in /home/user and follow the link /home/user/link and do 'cd ..', you will be back in /home/user.

I mean if I make something like

$ ln -s ~/linux/aur ~/aur

then

$ cd ~
$ cd aur
$ cd ..

that will take me to ~/linux not ~/

Whereas if hard linking was possible on directories I could create two separate paths in the filesystem. eg:

                        --- linux --- aur
--- home --- user --- {
                        --- aur

I don't think what I want is possible without learning C then learning how to hack the kernel.... hmm

Offline

#10 2014-01-30 19:46:21

ANOKNUSA
Member
Registered: 2010-10-22
Posts: 2,141

Re: Creating Hard Links for Directories

You might consider dantalian.  The developer created it specifically for this purpose--using hard links to organize a large music collection---although you can organize any collection of files. You'd have to be selective about how you used your tags to achieve your specific goal, but I think the idea's the same.

Offline

#11 2014-01-31 11:29:57

frostschutz
Member
Registered: 2013-11-15
Posts: 1,382

Re: Creating Hard Links for Directories

which shell are you using? I have a ~/www/ symlink that points to /var/www/...

yet cd ~; cd www; cd ..; puts me back in ~, and not ever in /var/www.

Offline

#12 2014-01-31 23:32:48

nullAtman
Member
Registered: 2014-01-23
Posts: 4

Re: Creating Hard Links for Directories

Thanks for the link ANOKNUSA. I'll check it out.

frostschutz, I was using the fish shell. Bash worked fine smile

Offline

Board footer

Powered by FluxBB