LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack 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: 62
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


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find symlink target name in script germanyzulu Linux - Software 3 08-09-2006 03:14 PM
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


All times are GMT -5. The time now is 05:39 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration