Folder structure as (file.txt are plain text files in each directory, not symlinks):


The same file.txt that sits in /mydir:


I need a command that would run from /basedir that:

  • Finds recursively all /file.txt (in .A/, ./B and ./C)
  • Removes found file.txt files
  • Creates a symlink to /mydir/file.txt

Can that be done in a single bash command?

Thank you!

  • Is file.txt the same file over all the directories or just any .txt ? – M. Becerra May 13 '17 at 15:22
  • Yes, it's exactly the same file. – temuri May 13 '17 at 15:45

This will do it:

find /basedir -type f -name file.txt -exec ln -sf /mydir/file.txt '{}' \;

This finds all files named file.txt under /basedir, and runs ln on each to replace it (-f) with a symbolic link (-s) to /mydir/file.txt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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