1

I have a symlink to a file on my Ubuntu system, and I need to copy the original file to a different directory and have a new name there. I am able to copy it to a different directory using

readlink -ne my_symlink | xargs -0 cp -t /tmp/

But I am not able to give a new name in the destination directory. Basically, I am looking for a command that could look like:

readlink -ne base.txt | xargs -0 cp -t /tmp/newnametofile

When I try the exact same command above, it gives me file or directory not found error.

Anyway to achieve this?

terdon
  • 234,489
  • 66
  • 447
  • 667

1 Answers1

3

cp will dereference symlinks with -L option.

This should work:

cp -L my_symlink /tmp/newnametofile

Regarding your xargs, -t, --target-directory option of cp only takes DIRECTORY as input. You could make it work using xargs -I{} cp {} /tmp/newnametofile (but I'd use cp -L anyways...

pLumo
  • 22,231
  • 2
  • 41
  • 66