A friend of mine points out that if you do:
perl -pi.bak -e 's/foo/bar/' somefile
when "somefile" is actually a symlink, perl does just what the docs say it will do:
It does this by renaming the input file, opening
the output file by the original name, and selecting that
output file as the default for print() statements. The extension, if
supplied, is used to modify the name of the old file to
make a backup copy […]
Which results in a new symlink "somefile.bak" pointing to the unchanged real file, and a new, changed regular file "somefile" with the changes.
In many cases, following the symlink would be the desired behavior (even if it leaves the correct location of the .bak file ambiguous). Is there a simple way to do this other than testing for symlinks in a wrapper and handling the case appropriately?
(sed
does the same thing, for what that's worth.)
Best Answer
I wonder whether the small
sponge
general-purpose utility ("soak up standard input and write to a file") from moreutils will be helpful in this case and whether it will follow the symlink.The author describes
sponge
like this: