Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-25-2007, 06:54 AM   #1
LQ Newbie
Registered: Nov 2005
Posts: 11

Rep: Reputation: 0
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?
Old 01-25-2007, 07:04 AM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find symlink target name in script germanyzulu Linux - Software 4 08-14-2015 10:23 AM
Tip: How to copy a symlink and its target file in a script fotoguy Programming 4 08-05-2006 03:24 PM
CLI copy is greater than GUI copy ?? 0_o killahsmurf Slackware 14 01-04-2006 11:53 AM
Symlink Fredde87 Linux - General 14 09-30-2005 08:06 PM
boot error after using Copy Commander to copy debian system onto larger new drive Interceptor Linux - Hardware 7 05-04-2003 12:40 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:14 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration