# Working with symlink and learning unlink = rm the hard way



## tekkon (Aug 21, 2009)

I was following this thread to mount ntfs-3g at boot time. The solution involves creating a symlink from ntfs-3g to mount_ntfs which I'm not familiar with.

I'm on FreeBSD 7.1-RELEASE. I'll describe what i did to my system.

I tried creating a symlink by putting

```
ln -s /sbin/mount_ntfs /usr/local/bin/ntfs-3g
```
in '/etc/rc.local'. I gotten this error:

```
ln: /usr/local/bin/ntfs-3g: File exists
```
at boot.

I read the ln man page some more and tried:

```
ln -sf /sbin/mount_ntfs /usr/local/bin/ntfs-3g
```
It worked. Now I learn I've gotten the order of the source and target of the linking wrong. I then issued:

```
ln -sf /usr/local/bin/ntfs-3g /sbin/mount_ntfs
```
which is the correct order I needed.

I issued the following to check the symlink. I got:

```
file /sbin/mount_ntfs
/sbin/mount_ntfs: broken symbolic link to `/usr/local/bin/ntfs-3g'
```
Then I checked /usr/local/bin/ntfs-3g and gotten the same error:

```
file /usr/local/bin/ntfs-3g
/usr/local/bin/ntfs-3g: broken symbolic link to `/sbin/mount_ntfs'
```

It seems I've now linked both files in a circle to each other. So I looked for a tool to unlink them. I issued:

```
unlink /sbin/mount_ntfs
```
and now it appears I had deleted /sbin/mount_ntfs with unlink. 

Figuring that until I find a way to replace the deleted /sbin/mount_ntfs, I should just remove the broken symlink of /usr/local/bin/ntfs-3g from /sbin/mount_ntfs at this time. I made a backup this time with:

```
cp -RP /usr/local/bin/ntfs-3g /usr/local/bin/ntfs-3g_bak
```
this time in case the symlink remove procedure goes wrong again.

After issuing:

```
rm /usr/local/bin/ntfs-3g
```
surely not only the symlink but the file 'ntfs-3g' was also gone. I recovered 'ntfs-3g' from the backup to find that the broken symlink from '/sbin/mount_ntfs' has also been restored.

That was what happened. Please teach me how to safely remove symlinks between 2 binary files without removing the file itself and how to replace /sbin/mount_ntfs that I've deleted.


----------



## Alt (Aug 21, 2009)

1. Do not use `-f` flag unless you need to FORCE action. That why you overwrited ur real binary..
2. If you do `ln -s /usr/local/bin/ntfs-3g /sbin/mount_ntfs` one time, this will create symlink "/sbin/mount_ntfs" pointing to your binary. It is *no need* to do it every boot. (If your /sbin/ is not a ramdisk lol))
3. Do not put any stuff to /etc/rc.local or /etc/rc.d or /etc anyway =) Its not linux, user startup scripts must live in /usr/local/etc/rc.d/
4. If you do `rm /sbin/mount_ntfs` it will remove object "/sbin/mount_ntfs" - no matter symlink or file. Real file not affected. (Caution if you symlink a directory and then `rm -rf` it can destroy real files)


----------



## tekkon (Aug 21, 2009)

Thank you for your advices.

I used -f with ln because using -s only returned the error: 'ln: /usr/local/bin/ntfs-3g: File exists'

As symlinks are created one time, how do I get a list of the symlinks created besides having to check the individual linked files?

Since I had symlinked one binary to another, removing the symlink will remove the symlink will remove the linking binary too. Is there a way to remove the symlink only without touching the binary?


----------



## Alt (Aug 21, 2009)

tekkon said:
			
		

> I used -f with ln because using -s only returned the error: 'ln: /usr/local/bin/ntfs-3g: File exists'


It protected you, but you forced =)



			
				tekkon said:
			
		

> As symlinks are created one time, how do I get a list of the symlinks created besides having to check the individual linked files?


dont understood =)



			
				tekkon said:
			
		

> Since I had symlinked one binary to another, removing the symlink will remove the symlink will remove the linking binary too. Is there a way to remove the symlink only without touching the binary?


Read part 4 above =)


----------



## jb_fvwm2 (Aug 21, 2009)

I've put a working example of symlink creation in /etc/motd (along
with other commands) so that I can grep the proper usage instantly...
(Sorry to not answer the question(s)...)
(ignoring the post immediately above, it was posted as I wrote)


----------



## tekkon (Aug 22, 2009)

Alt:
Okay, I think I learn something new about symlinks. A symlinks is an object and cannot exist together with another object of the same name along the same path.

Since I had already issued 'ln -sf' on both '/sbin/mount_ntfs' and '/usr/local/bin/ntfs-3g' as targets, that means both mount_ntfs and ntfs-3g now is a symlink instead of the original binary.

I think reinstalling ntfs-3g can get the ntfs-3g binary back but how do I restore mount_ntfs ?

From what I had seen, the correct way to symlink the source 'ntfs-3g' to target 'mount_ntfs' is to issue:

```
mv /sbin/mount_ntfs /sbin/mount_ntfs.bak
```
before running

```
ln -s /usr/local/bin/ntfs-3g /sbin/mount_ntfs
```
am I right?



			
				Alt said:
			
		

> dont understood =)


I thought the system had a list somewhere keeping track of all the symlinks that exist throughout the system.

jb_fvwm2:
Thanks for the good tip.


----------



## phoenix (Aug 22, 2009)

The argument order for ln is the same as for cp or for mv.  In other words, you link *from* an existing file *to* the name of the link.

The correct ln command is `# ln -s /usr/local/bin/ntfs-3g /usr/bin/mount_ntfs`.

To remove a symlink is as simple as removing a file, just rm it: `# rm /usr/bin/mount_ntfs`  After all, a symlink is just a file.

What happened, in your case, is that you wrote the arguments backwards.  And it gave an error.  Which you then ignored, and forced the link from a non-existent file overtop of an existing file (which wiped out the contents of ntfs-3g).  Then you deleted the non-existent symlink, leaving an empty file behind.  

You'll need to re-install the ntfs-3g port to get things working again.  And then re-do the symlink, with the arguments in the right order.


----------



## Alt (Aug 22, 2009)

tekkon said:
			
		

> From what I had seen, the correct way to symlink the source 'ntfs-3g' to target 'mount_ntfs' is to issue 'mv /sbin/mount_ntfs /sbin/mount_ntfs.bak' before running 'ln -s /usr/local/bin/ntfs-3g /sbin/mount_ntfs' am I right?


Yes.



			
				tekkon said:
			
		

> I thought the system had a list somewhere keeping track of all the symlinks that exist throughout the system.


Technically, a symlink is just a file. So, it lives in your filesystem =) Difference is that it have attribute saying it is symlink. In this case file content is just a path to original file which it point to.
Look this:
	
	



```
> ls -la s.tgz
lrwxr-xr-x  1 alt  wheel  14 22 Ð°Ð²Ð³ 09:20 s.tgz -> sitemap.xml.gz
```
We do an ls and see "lrwxr-xr-x". 'l' says its symlink. Filesize is 14 cus this file is a symlink and content only path to "sitemap.xml.gz"


----------



## tekkon (Aug 22, 2009)

Alt, phoenix:

Thanks for further explaining. I now have a better understanding of how to work with symlinks.


One last thing though, how do I replace the '/sbin/mount_ntfs' binary that I had destroyed? Since 'mount_ntfs' is part of the base system, do I need to rebuild my FreeBSD 7.1-RELEASE to get mount_ntfs back?


----------



## Beastie (Aug 22, 2009)

If you still have the ISO/CD, why don't you just get it back from there? It's in the /7.x-RELEASE/base directory. You'll have to merge the chunks and untar the merged file. Check install.sh for more information. You'll find mount_ntfs in /sbin obviously.

EDIT: if you don't have the ISO anymore, you can also get the base from the ftp: ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/7.x-RELEASE/base/ (change the x to your release minor version)


----------



## phoenix (Aug 22, 2009)

If you have the source tree installed, you should be able to go into /usr/src/sbin/mount_ntfs and run `# make; make install` to re-install it.


----------

