预期情景:
$ ls -lh /lib/libcurl.so*
lrwxrwxrwx 1 root root 16 Mar 26 2013 /lib/libcurl.so -> libcurl.so.4.0.1
lrwxrwxrwx 1 root root 16 Mar 26 2013 /lib/libcurl.so.4 -> libcurl.so.4.0.1
-rwxr-xr-x 1 root root 1.5M Mar 26 2013 /lib/libcurl.so.4.0.1
$ copy_with_links /lib/libcurl.so.4 /tmp/libcurl/
$ ls -lh /tmp/libcurl/
lrwxrwxrwx 1 root root 16 Mar 18 2014 /tmp/libcurl/libcurl.so -> libcurl.so.4.0.1
lrwxrwxrwx 1 root root 16 Mar 18 2014 /tmp/libcurl/libcurl.so.4 -> libcurl.so.4.0.1
-rwxr-xr-x 1 root root 1.5M Mar 18 2013 /tmp/libcurl/libcurl.so.4.0.1
答案1
自己编码:https://gist.github.com/vi/adb4e81d2d8750d2c949
#!/bin/bash
if [ -z "$2" ]; then
echo "Usage: copy_with_links one_or_more_source_files target_directory/"
exit 1
fi
set -e
f=("$@")
d=${f[${#f[@]}-1]}
d=$(readlink -f "$d")
# remove the target directory from list of files
unset f[${#f[@]}-1]
for a in "${f[@]}"; do.
b=$(readlink -f "$a")
dn=$(dirname "$b")
for q in $(find -L "$dn" -maxdepth 1 -mindepth 1 -samefile "$b"); do
cp -ai "$q" "$d/"
done
done
答案2
我通常只做cp -a <FILES...> <DEST_DIR>
。-a
尝试保留尽可能多的文件属性(包括将符号链接保留为符号链接),但如果您想要更精细的粒度,还有其他标志可以做得更少。