将符号链接replace为目标

如何用Mac OS X上的目标replace目录(和子目录)中的所有符号链接? 如果目标不可用,我宁愿单独离开软链接。

如果您使用的是Mac OSX别名,请find . -type l find . -type l不会拿出任何东西。

您可以使用以下[Node.js]脚本将符号链接的目标移动/复制到另一个目录:

 fs = require('fs') path = require('path') sourcePath = 'the path that contains the symlinks' targetPath = 'the path that contains the targets' outPath = 'the path that you want the targets to be moved to' fs.readdir sourcePath, (err,sourceFiles) -> throw err if err fs.readdir targetPath, (err,targetFiles) -> throw err if err for sourceFile in sourceFiles if sourceFile in targetFiles targetFilePath = path.join(targetPath,sourceFile) outFilePath = path.join(outPath,sourceFile) console.log """ Moving: #{targetFilePath} to: #{outFilePath} """ fs.renameSync(targetFilePath,outFilePath) # if you don't want them oved, you can use fs.cpSync instead 

这里是使用readlinkchmeee的答案的版本,并且如果任何文件名中有空格,它们将正常工作:

新文件名等于旧链接名称:

 find . -type l | while read -r link do target=$(readlink "$link") if [ -e "$target" ] then rm "$link" && cp "$target" "$link" || echo "ERROR: Unable to change $link to $target" else # remove the ": # " from the following line to enable the error message : # echo "ERROR: Broken symlink" fi done 

新文件名等于目标名称:

 find . -type l | while read -r link do target=$(readlink "$link") # using readlink here along with the extra test in the if prevents # attempts to copy files on top of themselves new=$(readlink -f "$(dirname "$link")/$(basename "$target")") if [ -e "$target" -a "$new" != "$target" ] then rm "$link" && cp "$target" "$new" || echo "ERROR: Unable to change $link to $new" else # remove the ": # " from the following line to enable the error message : # echo "ERROR: Broken symlink or destination file already exists" fi done 

你没有说replace后文件应该有什么名字。

这个脚本认为被replace的链接应该和链接有相同的名字。

 for link in `find . -type l` do target=`\ls -ld $link | sed 's/^.* -> \(.*\)/\1/'` test -e "$target" && (rm "$link"; cp "$target" "$link") done 

如果你想让文件与目标名称相同,就应该这样做。

 for link in `find . -type l` do target=`\ls -ld $link | sed 's/^.* -> \(.*\)/\1/'` test -e "$target" && (rm $link; cp "$target" `dirname "$link"`/`basename "$target"`) done