pax
pax

Reputation: 90

Copy symbolic link to same file name

I think the reason I can't find how to do this is just because I don't know the jargon.

At my job I often need to get rid of symbolic links (to our design management database). This is because people Unix copy the directories over and need to harden the files (not sure if that is best term). OK, example:

$ ls -l
file1 -> /something/asdf/1235
file2 -> /something/fdsa/5431

Then I have to:

rm file1
cp /something/asdf/1235 file1
rm file2
cp /something/fdsa/5431 file2

It can be tedious and seems like something fundamental.

Is there a on liner so I can do this on a hundreds of files at once? Also, can someone educate me on the jargon I'd need to ask this question without an example, like "I need to turn a symbolic link into a __". Thank you!

Upvotes: 1

Views: 2869

Answers (1)

dogbane
dogbane

Reputation: 274622

It can be done like this:

cp --remove-destination "$(readlink file1)" file1

The readlink command gives you the name of the file that the symlink is pointing to. The cp command then performs the copy, but removes the symlink first.

Upvotes: 6

Related Questions