2

I want to remove the symlink and instead insert the file it links to, if possible all with one command so I can have it as an "External Tool" in phpStorm.

0
cp --remove-destination "$(readlink <symlink>)" <symlink>

Source: https://unix.stackexchange.com/a/35027

6
  • Thanks I tried this and it didn't work but it turns out that it was because the links where copied and didn't point to the right place anymore. But I was confused and thought it was something with bash
    – OZZIE
    Nov 1 '14 at 13:49
  • How do I get this to work for all symlinks in a directory? Find complains about illegal option -t whenever I put it in a loop.
    – cjm
    Jan 16 '17 at 18:22
  • @cjm This should work fine with find. Are you missing the path as an argument to find? What's your exact find command?
    – grg
    Jan 16 '17 at 18:25
  • @grgarside I was missing the path as my find argument. Now my problem is that cp is getting whiny about illegal option -- - instead of recognizing --remove-destination as a valid option. Command is for f in (find . -type l); cp --remove-destination "(readlink $f)" $f;end;; shell is fish. It complains about the illegal option whether or not the (readlink $f) is quoted or not.
    – cjm
    Jan 16 '17 at 18:33
  • 1
    I was using the shell for looping based off this answer. It seems that --remove-destination doesn't work in Mac OS, since everything I try ends with cp: illegal option -- -
    – cjm
    Jan 16 '17 at 18:58
0

Since macOS' cp doesn't have the --remove-destination parameter, you have a couple options:

  • Install GNU cp

    brew install coreutils
    

    And then follow the step in grg's answer (note that GNU cp is installed as gcp)

    gcp -a --remove-destination "$(readlink <symlink>)" "<symlink>"
    
  • Or remove the symlink first, e.g.

    sourcefile=$(readlink "<symlink>"); rm "<symlink>" && cp -a "$sourcefile" "<symlink>"
    

You must log in to answer this question.

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