LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 10:28 PM   #1
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
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, 04:31 PM   #2
Brian1
LQ 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: 65
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, 09:19 PM   #3
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Original Poster
Rep: Reputation: 62
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, 02:45 PM   #4
Brian1
LQ 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: 65
Very confusing to me. Sorry can't be of more help.

Brian1
 
Old 08-05-2006, 03:24 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

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

Tar is keeping plain files and links as they are.
 
  


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
Copy file by script python ! BongHongXanh Programming 2 01-11-2006 08:26 PM
shell script to copy lines from a file Warmduvet Programming 2 09-14-2004 09:25 PM
Run script during file copy or create in directory neranjana Linux - General 1 01-13-2004 06:57 AM
copy 1 file to multiple dir script carpman Linux - General 14 02-07-2002 09:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:59 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
Open Source Consulting | Domain Registration