This question already has an answer here:

I have two directories: /all/origins/ & /all/links/.

Everything in links/ points individually to something in origins/, but not everything in origins/ is linked in links/. (Squares, rectangles & categorical hierarchy, get it?) These are symlinks (ln -s), not hard links.

Everything in 1. links/ needs to be deleted, along with 2. the symlink destinations in origins/ (not knowing whether they are in origins/), and 3. the links/ directory itself.

What rm Unix command should I type to remove both the entire links/ directory AND any symlinked destinations symlinked to in origins/?

I'm looking for something maybe like: rm -R /all/links or rm -r --follow-all-ln /all/links.

If an rm command with parameters will not do the job, please say so explicitly as that is my preference and implicit in the title (can Unix & Linux do this via rm without a do loop?—only if that won't work, then please state so and explain what do loop.)

marked as duplicate by RalfFriedl, G-Man, elbarna, Thomas, schily Nov 23 '18 at 13:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    In third line I think you want to say links/ instead of origins/. – Prvt_Yadv Nov 22 '18 at 23:23
  • Possible and related, but not coterminous. My question is about removing the directory itself and such links with it. This near-dup removes only a file and only one at a time, specifically. But, it is related, so thanks. – Jesse Steele Nov 23 '18 at 1:43

First take backup of your data in case something goes wrong.

What I understood is that you want to delete all the contents of /all/links/ directory and the file to which the links inside this directory are pointing.

There is no way to do this with a simple Unix command as you preferred, but it will work with a simple script.


for i in /all/links/*
rm  "$(readlink -f $i)"
rm "$i"

rmdir /all/links

As you said that all files inside /all/links/ are symbolic links so there is no need to check each value of i whether it is a link or not.

  • Is there no way to do this with a simple Unix command? If so, I accept this as the answer and please approve my edit. – Jesse Steele Nov 23 '18 at 3:21
  • @JesseSteele the script is short enough that you could write it in a single line of shell... for f in /all/links/*; do rm "$(readlink -f $f)"; rm "$f"; done; rmdir /all/links. If you need it possibly more than once... go for the script as script. – planetmaker Nov 23 '18 at 9:39
  • Thank you, make it an answer and I will up vote it for usefulness, but, honestly, it is still not a simple Unix command (part of the question), but a script on a single line – Jesse Steele Nov 23 '18 at 9:41
  • @JesseSteele actually I replaced your edit rm -R /all/links with rmdir /aa/links because rm $i is deleting every file, so after the for loop the directory will be empty and there is no need to use recursion. – Prvt_Yadv Nov 23 '18 at 9:45
  • That is incorrect because there may be files other than links in the dir to be deleted, which was vague in the OP, but possible. Is all that ok? I don't want to edit the OP for this, to avoid the #chameleon thing. – Jesse Steele Nov 23 '18 at 10:23

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