LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   removing symbolic link target (https://www.linuxquestions.org/questions/linux-general-1/removing-symbolic-link-target-949275/)

bilyboy65 06-08-2012 03:34 PM

removing symbolic link target
 
I'm sorry if this is a duplicate, but most of the posts I could find on this subject pertains to removing a symbolic link.

I have 2 symbolic links
current -> /home 1.3
previous -> /oldhome 1.2

I have a bash script that copies the current to the previous. Before I do that though I want to remove the previous. I don't want to just remove the link. I also want to remove the target of the link. The problem is that I don't know where the previous link is pointing to because it's a versioned directory and I won't know what the version is within the script.

If deleting the links target isn't possible, I can solve this problem if anyone can tell me how to print out what the link is pointing to.
****update****
I've just discovered readlink which returns the directory it's pointing to. With that I can solve my issue, but I'd still like to know if deleting a symbolic link's target through the link is possible.

For example (I know this doesn't work)
echo previous
returns: /oldhome 1.2

Thank you for any help.

pixellany 06-08-2012 03:50 PM

How about inserting some diagnostics in the script to tell you exactly what it's doing?

bilyboy65 06-08-2012 03:58 PM

Quote:

Originally Posted by pixellany (Post 4699059)
How about inserting some diagnostics in the script to tell you exactly what it's doing?

What diagnostics would you suggest? For the sake of the argument lets just say that this is all the script is trying to do, remove a link's target without actually knowing it.

avarus 10-10-2012 12:06 PM

Hi,

For the benefit of anyone else visiting this thread, the final answer is:

rm -r "`readlink -f your_link`"

The "-f" means this will work even through more than one layer of symlinks, removing the final file or directory.
The funny quoting armours the backticks against spaces in file or directory names.

I don't believe there is a one-shot command to do this. Certainly GNU rm has no "--dereference" option.

TIM

bilyboy65 10-10-2012 12:25 PM

Quote:

Originally Posted by avarus (Post 4802339)
Hi,

For the benefit of anyone else visiting this thread, the final answer is:

rm -r "`readlink -f your_link`"

The "-f" means this will work even through more than one layer of symlinks, removing the final file or directory.
The funny quoting armours the backticks against spaces in file or directory names.

I don't believe there is a one-shot command to do this. Certainly GNU rm has no "--dereference" option.

TIM

Hi Tim,

Thanks for that. Luckily that wasn't something I needed or I may have been searching for that for a while as well. Marking this as complete now.


All times are GMT -5. The time now is 12:12 AM.