Is there a way to test if a file has a symlink to it?

Brazen

Diamond Member
Jul 14, 2000
4,259
0
0
This is on Debian Etch. I'm wondering if there is a test I could run against a file to tell me if there are any symlinks pointed at that file.

I'm also wondering, what would happen to a symlink, if the file it is pointed to is deleted. Would the symlink just become invalid?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Tracking symlinks would be a nightmare. For example think about all the possibilities involving nfs and multiple clients. You could quite easily have a link that's valid on one machine and invalid on another at the same time even.
 

silverpig

Lifer
Jul 29, 2001
27,703
12
81
Originally posted by: Brazen
This is on Debian Etch. I'm wondering if there is a test I could run against a file to tell me if there are any symlinks pointed at that file.

I'm also wondering, what would happen to a symlink, if the file it is pointed to is deleted. Would the symlink just become invalid?

Yeah, the symlink just breaks. Try it if you want

echo "1" > test
ln -sf test link
cat link (you should see "1" sans quotes)
rm test
cat link

That gives "cat: cannot open link" to me.

As for monitoring if there are symlinks, I thought the same thing as kamper. One system storing a file might not know if another system has linked to one of its files via nfs or the like.
 

Brazen

Diamond Member
Jul 14, 2000
4,259
0
0
Originally posted by: silverpig

As for monitoring if there are symlinks, I thought the same thing as kamper. One system storing a file might not know if another system has linked to one of its files via nfs or the like.

That's not an issue for my situation. I'm only concerned with local symlinks. I think hardlinks might be better for me.

I'm trying to clean up wasted space caused by duplicate files on our file server. User's "My Documents" folders are stored on our file server and I know a lot of people have the same files copied into their My Documents.

I think I, theoretically, have an idea of how it will work:

1. Use fdupes to find duplicate files
2. Move those files to a central location
3. Create links (probably hardlinks?) from the original locations to the new central location

The biggest problem is... What about when a user opens that file and edits it? Even if I use hardlinks, then wouldn't it change everyone's file?

The reason I though I would need to check for symlinks is, I figured I would run a nightly script to see if all the symlinks to a file were deleted, then the file could be deleted, but with hardlinks that shouldn't be an issue (if I am understanding hardlinks correctly).
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: Brazen
Originally posted by: silverpig
As for monitoring if there are symlinks, I thought the same thing as kamper. One system storing a file might not know if another system has linked to one of its files via nfs or the like.

That's not an issue for my situation. I'm only concerned with local symlinks.
Then you'd have to treat symlinks to files on the same partition differently than ones to files on other partitions/slices.
I think hardlinks might be better for me.

I'm trying to clean up wasted space caused by duplicate files on our file server. User's "My Documents" folders are stored on our file server and I know a lot of people have the same files copied into their My Documents.

I think I, theoretically, have an idea of how it will work:

1. Use fdupes to find duplicate files
2. Move those files to a central location
3. Create links (probably hardlinks?) from the original locations to the new central location

The biggest problem is... What about when a user opens that file and edits it? Even if I use hardlinks, then wouldn't it change everyone's file?
Yes.
The reason I though I would need to check for symlinks is, I figured I would run a nightly script to see if all the symlinks to a file were deleted, then the file could be deleted, but with hardlinks that shouldn't be an issue (if I am understanding hardlinks correctly).
You'd also have to guarantee that all data is always on the same partition/slice. And if you were to do this, I don't see the sense in moving the files to a central location, that just creates an additional hard link. Assuming the editing wasn't a problem, if two users have the same file, just hard link one to the other and when the last user deletes the file, it will disappear on its own.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
The biggest problem is... What about when a user opens that file and edits it? Even if I use hardlinks, then wouldn't it change everyone's file?

The same thing would happen with symlinks though, symlinks don't do any copy-on-write magic.

The reason I though I would need to check for symlinks is, I figured I would run a nightly script to see if all the symlinks to a file were deleted, then the file could be deleted, but with hardlinks that shouldn't be an issue (if I am understanding hardlinks correctly).

You can do it, but you'd have to search every filesystem and look at every symlink's destination. Go ahead and run the symlinks command across your system and see how long it takes and then add on time to track all of those symlinks and compare them to the files you want to delete. It's possible, but it's not a great idea.