3

I have files with symlinks as shown below :

link_AB91 -> file_12857.xml
link_XY99 -> file_102856.xml
link_EF02 -> file_96843.xml

In my script I reference the link not the actual target file. However, when I perform the copy I want the actual file copied over. I am looking for an option and/or command which will allow me to achieve this.

Example :

cp "some_option_here" link_AB91 .

This should copy file file_12857.xml not the link link_AB91
I have tried options -L and -H but they do not help
Note that I do not want the -d or -P option either.

Update : If not option exists, then I will use readlink command but I am hoping there is a quicker way of performing this task

souser
  • 187
  • 1
  • 3
  • 8

2 Answers2

6

Use readlink /path/to/link to get the real file

cp `readlink /path/to/my/link` /foo/destination

equals

cp $(readlink /path/to/my/link) /foo/destination

moestly
  • 1,138
  • 8
  • 10
0

A minor adjustment to @ansi_lumen's answer:

cp `readlink -e /path/to/my/link` /foo/destination

This makes a difference for relative links. It makes sure that cp gets an absolute path to the file instead of a relative path.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
  • cp: invalid option -- 'e' - which CP are you referencing? – djdomi Aug 03 '21 at 13:53
  • 1
    @MichaelHampton It outputs the whole path that "cp" requires. If for instance link points to a relative path the "-e" will make readlink output the absolute path. @djdomi You forgot the `` operators. The -e is for readlink. – Arnie Schraufenziger Aug 04 '21 at 10:03