Today I discovered the awesome Create Symlinks action from JuneCloud. It works great, but creates duplicate files (appending a number to each one) for any symlink that already exists in the final location. I want it to just ignore those files and keep on trucking.

I tried using a shell script ln -s but that just plain fails with a The action “Run Shell Script” encountered an error. message after hitting a file that already exists.

Is there a way to create a symlink but SKIP existing files? I know I can us -f to overwrite but I'd rather just skip. How can this be done either in Automator or shell script?


Yes! You can do it easily in the shell script. The -h flag fits the bill, as described (sort of) on the ln man page. So instead of doing ln -s, do ln -sh.

Here's an example. Say you wanted to make a symbolic link of each of your downloads, which would exist on your Desktop, but you already have some symbolic links there. Then you could do this:

cd Downloads
for f in *; do ln -sh ~/Downloads/"$f" ~/Desktop; done;

For any files that you have that you already have symbolic links for, the command will print out:

    ln: /Users/******/Desktop/FlashPlayer.dmg: File exists

The command does this printing after running so it does not throw any errors nor require any additional inputs.

| improve this answer | |
  • OHH! Well damn. I read the man (believe it or not! lol) but where it says that -h causes ln to "not follow" the link if it exists, I didn't take that to mean "skip and keep going". Unfortunately it's not working, Automator still barfs because "file exists". =( – JVC Apr 18 '17 at 22:15
  • @JonathanvanClute Yeah it's confusing – NoahL Apr 18 '17 at 22:16
  • Any idea why Automator still doesn't like it? – JVC Apr 18 '17 at 22:23
  • I don't know, except you might want to try running the script by itself (w/o Automator) and making sure it works like that. – NoahL Apr 18 '17 at 22:25
  • 1
    Holy crap I fixed it! Thanks to this web page: tewha.net/2012/07/make-a-symbolic-link-from-finder - I happened to notice a small detail in their version of the ln script - a shift; after the ln command. Just for the hell of it I put it in and voila - no errors at all, and the script performs exactly as I had hoped it would. Eureka! =) – JVC Apr 18 '17 at 23:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .