LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-03-2006, 11:28 PM   #1
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: KirraMail Live Email Server
Posts: 1,280

Rep: Reputation: 61
Tip: How to copy a symlink and its target file in a script


Just a brief history on what I have been doing, I trying to make some stand alone applications like abiword, gaim, pan etc.. for a mini version of slackware 10.0. I can find all the shared libraries by using the ldd command no problem but when it comes to copying them, that's where the problems start.

When using a script if I use cp or rsync I can only either copy the symlink and not the target file of the symlink, or it creates the symlink as the target file. But I eventually found a way around it with the cp command.

For an example if I want to copy /lib/ld-linux.so.2 which is a symlink to /lib/ld-2.3.2.so the command to use would be:

Code:
cp -R /lib/ld-linux.so.2 /some/directory
cp -H /lib/ld-linux.so.2 /some/directory
I found that the -R switch would copy the symlink only and then the -H switch would then copy only the target file. I tried it with both switches on the same line:

Code:
cp -HR /lib/ld-linux.so.2 /some/directory
In a few combinations but found it would not work, not too sure how much a tip this is, but thought if any one was out there trying to do something similar it may help you out some what. I tried googling a few days but could not find any info to successfully copying symlinks and their targets.
 
Old 08-04-2006, 05:31 PM   #2
Brian1
Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 61
Not sure if I understand your question but if you mean you want to copy a symbolic link file to another location then why not use the ' ln -s ' command to just create a new symbolic link in the new location from the orginal file. Then if you need to overwrite it use the -f ' ln -sf ' option for link command to force the overwrite.

Brian1
 
Old 08-04-2006, 10:19 PM   #3
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: KirraMail Live Email Server
Posts: 1,280

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by Brian1
Not sure if I understand your question but if you mean you want to copy a symbolic link file to another location then why not use the ' ln -s ' command to just create a new symbolic link in the new location from the orginal file. Then if you need to overwrite it use the -f ' ln -sf ' option for link command to force the overwrite.

Brian1
Sorry I didn't do a good job of explaining what I was doing. I'm making some standalone installation package for slackware 10.0 using makepkg, and to find all the required shared libraries use ldd.

It finds all the shared libraries required but some of these are actually symlinks. I created a script to copy all the libraries to a new location so I can have a clean directory for makepkg to build the new package.

But I found that cp or rsync seem to have a problem copying both the symlink and the target file. They seem to be able to only copy either the symlink itself or create the symlink with the contents of the target file.

Then after I built the package and installed it, when I ran /sbin/ldconfig to update all the shared libraries, it would get lots of errors about symlinks not being there.

Then using the commands above I was able to copy both the symlinks and their target files and build the standalone applications without any errors. Not really much of a tip, just something that had bugged me for a couple of days until I found a solution to it, and thought others who maybe having similar trouble with symlinks might benefit from some help.
 
Old 08-05-2006, 03:45 PM   #4
Brian1
Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 61
Very confusing to me. Sorry can't be of more help.

Brian1
 
Old 08-05-2006, 04:24 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Did you try using tar ?

Tar is keeping plain files and links as they are.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find symlink target name in script germanyzulu Linux - Software 3 08-09-2006 04:14 PM
Copy file by script python ! BongHongXanh Programming 2 01-11-2006 09:26 PM
shell script to copy lines from a file Warmduvet Programming 2 09-14-2004 10:25 PM
Run script during file copy or create in directory neranjana Linux - General 1 01-13-2004 07:57 AM
copy 1 file to multiple dir script carpman Linux - General 14 02-07-2002 10:29 AM


All times are GMT -5. The time now is 12:47 PM.

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