Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Last edited by bilyboy65; 06-08-2012 at 03:59 PM.
Reason: partial solution
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.
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
Last edited by avarus; 10-10-2012 at 12:07 PM.
Reason: typo
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.