Linux 如何将符号链接转换为常规文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8377312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert symlink to regular file?
提问by nibot
What is the most direct way to convert a symlink into a regular file (i.e. a copy of the symlink target)?
将符号链接转换为常规文件(即符号链接目标的副本)的最直接方法是什么?
Suppose filename
is a symlink to target
. The obvious procedure to turn it into a copy is:
假设filename
是到 的符号链接target
。将其转换为副本的明显程序是:
cp filename filename-backup
rm filename
mv filename-backup filename
Is there a more direct way (i.e. a single command)?
有没有更直接的方法(即单个命令)?
采纳答案by nibot
There is no single command to convert a symlink to a regular file. The most direct way is to use readlink
to find the file a symlink points to, and then copy that file over the symlink:
没有一个命令可以将符号链接转换为常规文件。最直接的方法是使用readlink
找到符号链接指向的文件,然后将该文件复制到符号链接上:
cp --remove-destination `readlink bar.pdf` bar.pdf
Of course, if bar.pdf
is, in fact, a regular file to begin with, then this will clobber the file. Some sanity checking would therefore be advisable.
当然,如果bar.pdf
实际上是一个普通文件,那么这将破坏文件。因此,建议进行一些健全性检查。
回答by Brian Cain
There's no single command. But if you don't want a copy and all you want is another reference, you can replace the symlink with a link (aka "hard link"). This only works if they're on the same partition, BTW.
没有单一的命令。但是,如果您不想要副本并且您想要的只是另一个参考,则可以用链接(又名“硬链接”)替换符号链接。这仅在它们位于同一分区时才有效,顺便说一句。
rm filename
ln target filename
回答by Andrii Radyk
cpcan remove the destination file:
cp可以删除目标文件:
cp --remove-destination target filename
回答by Paul Beusterien
On OSX
在 OSX 上
rsync `readlink bar.pdf` bar.pdf
rsync `readlink bar.pdf` bar.pdf
回答by kbrock
Just a rehash of other's answers, but adding the "sanity check" to ensure the link passed in is actually a symbolic link:
只是对其他人的答案的重新整理,但添加“健全性检查”以确保传入的链接实际上是一个符号链接:
removelink() {
[ -L "" ] && cp --remove-destination "$(readlink "")" ""
}
This is saying that if the file is a symbolic link, then run the copy command.
这就是说,如果文件是符号链接,则运行复制命令。
回答by user3879068
Rsync can nativly deference the symlink for you using -L flag.
Rsync 可以使用 -L 标志自然地为您考虑符号链接。
[user@workstation ~]$ rsync -h | grep -- -L
-L, --copy-links transform symlink into referent file/dir
It would be as simple as: rsync -L <symlink> <destination>
这将很简单: rsync -L <symlink> <destination>
回答by Yadhu
for f in $(find -type l);do cp --remove-destination $(readlink $f) $f;done;
- Check symlinks in the current directory and subdirectories
find -type l
- Get the linked file path
readlink $f
- Remove symlink and copy the file
cp --remove-destination $(readlink $f) $f
- 检查当前目录和子目录中的符号链接
find -type l
- 获取链接的文件路径
readlink $f
- 删除符号链接并复制文件
cp --remove-destination $(readlink $f) $f
回答by Trevor Hickey
Many have already stated the following solution:
许多人已经说明了以下解决方案:
cp --remove-destination `readlink file` file
However, this will not work on symbolically linked directories.
但是,这不适用于符号链接的目录。
Adding a recursiveand forcewill not work either:
添加递归和强制也不起作用:
cp -rf --remove-destination `readlink file` file
cp: cannot copy a directory, ‘path/file, into itself, ‘file'
cp:无法将目录“路径/文件”复制到自身“文件”中
Therefore, it is probably safer to delete the symlink entirely first:
因此,首先完全删除符号链接可能更安全:
resolve-symbolic-link() {
if [ -L ]; then
temp="$(readlink "")";
rm -rf "";
cp -rf "$temp" "";
fi
}
Not exactly a one-liner like you had hoped, but put this it into your shell environment, and it can be.
不像您希望的那样完全是单行的,但是将它放入您的 shell 环境中,它可以。
回答by Jared
This worked perfectly for me (edited to work with spaces):
这对我来说非常有效(编辑为使用空格):
find . -type l | while read f; do /bin/cp -rf --remove-destination -f $(find . -name $(readlink "${f}")) "${f}";done;
Allows you to recursively convert all symlinks under the current working folder to its regular file. Also doesn't ask you to overwrite. the "/bin/cp" exists so that you can bypass a possible cp -i alias on your OS which prevents the "-rf" from working.
允许您递归地将当前工作文件夹下的所有符号链接转换为其常规文件。也不要求您覆盖。"/bin/cp" 存在,以便您可以绕过操作系统上可能的 cp -i 别名,这会阻止 "-rf" 工作。
回答by dsclose
For text files the sed
command can do this in one line if you pass it the in-placeswitch (-i
). This is because sed
does a single pass over a file, cats the output into a temporary file which it subsequently renames to match the original.
对于文本文件,sed
如果您将就地开关 ( -i
)传递给它,则该命令可以在一行中完成此操作。这是因为sed
对文件进行单次传递,将输出转换为临时文件,然后重命名该文件以匹配原始文件。
Just do an inline sed
with no transforms:
只做一个sed
没有转换的内联:
sed -i ';' /path/to/symbolic/link