LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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

Reply
 
Search this Thread
Old 06-08-2012, 03:34 PM   #1
bilyboy65
LQ Newbie
 
Registered: Apr 2012
Posts: 14

Rep: Reputation: 0
Thumbs up 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.

Last edited by bilyboy65; 06-08-2012 at 03:59 PM. Reason: partial solution
 
Old 06-08-2012, 03:50 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
How about inserting some diagnostics in the script to tell you exactly what it's doing?
 
Old 06-08-2012, 03:58 PM   #3
bilyboy65
LQ Newbie
 
Registered: Apr 2012
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
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.
 
Old 10-10-2012, 12:06 PM   #4
avarus
Member
 
Registered: Apr 2004
Location: Oxford, UK
Distribution: Ubuntu, Debian, various
Posts: 230
Blog Entries: 5

Rep: Reputation: 32
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

Last edited by avarus; 10-10-2012 at 12:07 PM. Reason: typo
 
Old 10-10-2012, 12:25 PM   #5
bilyboy65
LQ Newbie
 
Registered: Apr 2012
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by avarus View Post
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.
 
  


Reply

Tags
remove, symbolic link


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
symbolic link not allowed or link target not accessible paullaubscher Linux - Newbie 4 03-10-2010 06:47 AM
How to create symbolic link to html file and avoid page link problem? haxpor Linux - General 2 01-29-2008 07:40 AM
Removing a symbolic Link michshelly Linux - Newbie 8 05-13-2006 08:26 AM
Removing a symbolic link lolipop Linux - Newbie 2 03-23-2005 11:53 AM
Help removing a symbolic link jamesmwlv Linux - Newbie 3 04-25-2003 11:51 AM


All times are GMT -5. The time now is 10:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration