![移动文件及其所有符号链接](https://linux22.com/image/851667/%E7%A7%BB%E5%8A%A8%E6%96%87%E4%BB%B6%E5%8F%8A%E5%85%B6%E6%89%80%E6%9C%89%E7%AC%A6%E5%8F%B7%E9%93%BE%E6%8E%A5.png)
对于我正在开发的图标主题,我把具有不同数量符号链接的不同文件放在不同的文件夹中。以下是其中一个示例:
FlatWoken/FlatWoken/scalable/apps$ find -L ../ -samefile bluetooth-active.svg
../stock/stock_bluetooth.svg
../status/bluetooth-active.svg
../status/blueman-active.svg
../apps/bluetooth-active.svg
../apps/blueradio-48.svg
../apps/bluetooth.svg
../apps/bluedun.svg
../apps/bluetoothradio.svg
../apps/preferences-system-bluetooth.svg
../apps/blueman.svg
我想要做的是找到一个终端命令,该命令能够将它们全部移动到新文件夹中(比如, FlatWoken/FlatWoken/24x24 ),而不会丢失符号链接(即 FlatWoken/FlatWoken/24x24/apps/bluedun.svg 应该符号链接到 FlatWoken/FlatWoken/24x24/apps/bluetooth-active.svg )。可以这样做吗?
为了进一步说明,如果我有这样的结构:
alecive@calliope:~/Scrivania/temp$ ls *
dest:
a
orig:
a b c
alecive@calliope:~/Scrivania/temp$ ls -l orig/*
orig/a:
totale 0
lrwxrwxrwx 1 alecive alecive 8 apr 9 10:22 link_a -> truefile
-rw-rw-r-- 1 alecive alecive 0 apr 9 10:22 truefile
orig/b:
totale 0
lrwxrwxrwx 1 alecive alecive 13 apr 9 10:23 link_b -> ../a/truefile
orig/c:
totale 0
lrwxrwxrwx 1 alecive alecive 13 apr 9 10:23 link_c -> ../a/truefile
alecive@calliope:~/Scrivania/temp$ ls -l dest/*
dest/a:
totale 0
-rw-rw-r-- 1 alecive alecive 0 apr 9 10:24 truefile
dest/b:
totale 0
dest/c:
totale 0
我想dest
用指向dest/a/truefile
(而不是orig/a/truefile
)的符号链接填充文件夹,而不使用绝对路径,而只使用相对路径。
提前感谢你的帮助!
答案1
先进材料。使用风险自负!您肯定需要修改它。如果您不理解它,请不要使用。
如果你能接受绝对符号链接,那么你可以这么做。假设你从这里开始:
[:~/tmp] % ls -l a b c orig
a:
total 0
lrwxrwxrwx 1 rmano rmano 16 Apr 1 16:53 linka -> ../orig/truefile
b:
total 0
lrwxrwxrwx 1 rmano rmano 16 Apr 1 16:53 linkb -> ../orig/truefile
c:
total 0
lrwxrwxrwx 1 rmano rmano 16 Apr 1 16:53 linkc -> ../orig/truefile
orig:
total 0
-rw-rw-r-- 1 rmano rmano 0 Apr 1 16:52 truefile
并且您想要移动下面的链接./dest
...
创建目标目录:mkdir dest
然后您可以使用这个脚本(将其保存为 script.sh,检查是否执行正确后删除 for 循环中的 echo):
#! /bin/bash
#
# $1 is the "true" file name, $2 the dest directory
# note both must exist, no check done! Let as an exrecise!
#
# put absolute path in $absf
absf="$(readlink -f "$1")"
#
# loop over all of the links we found under ".". Avoid the true file.
# Print instructions; remove the echo if you want to execute them
# (after checking, rm is not reversible)
#
for i in $(find -L . -samefile $1 | grep -v $1); do
echo ln -s $absf "$2"/"$(basename "$i")"
echo rm "$i"
done
...并运行它./script.sh orig/truefile dest/
。它将执行
ln -s /home/rmano/tmp/orig/truefile dest/linkb
rm ./b/linkb
ln -s /home/rmano/tmp/orig/truefile dest/linka
rm ./a/linka
ln -s /home/rmano/tmp/orig/truefile dest/linkc
rm ./c/linkc
你将拥有:
[:~/tmp/dest] % cd dest; ls -l
total 0
lrwxrwxrwx 1 rmano rmano 30 Apr 1 17:19 linka -> /home/rmano/tmp/orig/truefile
lrwxrwxrwx 1 rmano rmano 30 Apr 1 17:18 linkb -> /home/rmano/tmp/orig/truefile
lrwxrwxrwx 1 rmano rmano 30 Apr 1 17:19 linkc -> /home/rmano/tmp/orig/truefile
如果你需要相对符号链接,那么它会更复杂。你可以检查此链接开始编码...