-   Linux - Newbie (
-   -   Copy the target from a symlink? (

menator 01-25-2007 07:54 AM

Copy the target from a symlink?
I have a directory of symlinked files. I want to grab a copy of all the target files while retaining their original filename and not the one assigned to it by the symlink.

For example

I have a directory full of symlinks such as:

aaa.jar -> release/25-may-2006/aaa.jar.7

I wish to get a copy of aaa.jar.7.
If I use the normal cp command I can get the file called aaa.jar which will be the same as aaa.jar.7. However, I wish to get a copy of the target while keeping the original name (aaa.jar.7 in this case).

How can I do this?

matthewg42 01-25-2007 08:04 AM

You use the readlink command to get the name of the target. The result contains the path to the link, so if you just want the filename (not the full path), you need to remove anything preceeding the last /. You might use it like this.


cd /dir/with/all/your/symlinks
for f in *; do
    orig=$(readlink "$f")
    cp "$f" "/path/to/put/files/$orig_basename"

...which could copy all the files which were linked to from /dir/with/all/your/symlinks, and put them in /path/to/put/files/ with their original filenames. Be careful about over-writing files with duplicate names though... maybe you should add -i to the cp command.

All times are GMT -5. The time now is 11:54 AM.