LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-25-2007, 06:54 AM   #1
menator
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
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

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

Code:
cd /dir/with/all/your/symlinks
for f in *; do
    orig=$(readlink "$f")
    orig_basename="${orig##*/}"
    cp "$f" "/path/to/put/files/$orig_basename"
done
...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.
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:27 AM.

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