I have a symbolic link ~/Desktop/test.txt which points to ~/rendu/test.txt.

I know ONLY the path of ~/Desktop/test.txt, I want a fast way to delete ~/rendu/test.txt WITHOUT deleting the symbolic link.

For the one who want to know why: I have a file named crypted.xxx on my desktop, which is encrypted and contains my password.

When I want to update my encrypted file, I decrypt it and it create crypted.txt in a special directory. So I make a link to that file on my desktop for practical reasons. But after looking at my crypted.txt, I want to quickly delete this crypted.txt (but not the link in the desktop).

  • 4
    wait, you want to delete the file (as your question suggests) or delete the content of the file (as the title sais) – Jacob Vlijm Apr 5 '16 at 12:07
  • Delete the file and keep the symbolic link, sorry for misunderstanding – Ugo Hed Apr 5 '16 at 12:15
  • 2
    Maybe you could consider using a dedicated password manager instead? Keepass is pretty good, for example, and is in the repos. – evilsoup Apr 5 '16 at 18:42
up vote 11 down vote accepted

using find to find the symlink and then using readlink to get the full path to the target to rm:

find ~/Desktop/ -type l -name 'test.txt' -exec bash -c 'rm "$(readlink -f "$1")"' _ {} \;

Or as you know the link name already:

rm "$(readlink -f ~/Desktop/test.txt)"
  • 4
    Be careful with relative links though. A relative symbolic link is interpreted as relative to the directory it is located in. But your rm command will interpret it as relative to the current directory of the shell. Hence there is a possibility for that command to delete a different file from what the symbolic link was pointing to. – kasperd Apr 5 '16 at 14:36
  • 5
    @kasperd With -f -e or -m the full path is returned, even if the target symlink points to a relative path, so yes, that 's a concern in case of rm "$(readlink ~/Desktop/test.txt)", but not when using -f -e or -m. – kos Apr 5 '16 at 15:33
  • @kos You are right. In that case you only have to worry if there could be any race conditions. – kasperd Apr 5 '16 at 17:07
  • Alternatively, one can use realpath, which does in this case the same thing as readlink -f (but also works if the target itself is not a link). – leftaroundabout Apr 5 '16 at 22:47

Simply use:

printf "" > "$(readlink '/path/to/link')"

to clear the file, or

rm "$(readlink '/path/to/link')"

to remove the file.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.