Created
December 25, 2018 23:04
-
-
Save duzun/57a962adf6f2524ec40f0b8bdd5e2d49 to your computer and use it in GitHub Desktop.
Fix /usr/lib/lib*.so links, in the case you `cp`ed lib folder from a backup (use `rsync -a`, not `cp`!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
fix_lib_links() { | |
local i j dir write | |
[ "$2" = "-w" ] && write="$2" | |
dir=${1:-$PWD} | |
( | |
cd "$dir" && \ | |
for i in *.so.*; do | |
if [ -f "$i" ]; then | |
j="$i" | |
while [ "${j##*.}" != "so" ]; do | |
j="${j%.*}" | |
[ -L "$j" ] && break # stop at a link | |
if [ -f "$j" ]; then | |
# "*.so.0" could be a link to "*.so" | |
[ -L "$i" ] && [ "$(basename "$(readlink "$i")")" = "$j" ] && break; | |
if diff -q "$i" "$j" > /dev/null; then | |
echo "$j -> $i"; | |
[ -n "$write" ] && ln -sfT "$i" "$j"; | |
break; | |
fi | |
fi | |
done | |
fi | |
done | |
) | |
} | |
find_identic_files() { | |
local i j t p q dir write | |
[ "$2" = "-w" ] && write="$2" | |
dir=${1:-$PWD} | |
[ -d "$dir" ] || return 1 # no such folder | |
[ -d "$dir/.sha1" ] || mkdir -p -- "$dir/.sha1" || return 2 # can't create .sha1 folder | |
[ -d "$dir/.sha1twins" ] || mkdir -p -- "$dir/.sha1twins" || return 3 # can't create .sha1twins folder | |
( | |
cd "$dir" && \ | |
for i in *; do | |
if [ -f "$i" ] && [ ! -L "$i" ]; then | |
p="$(realpath "$i")" | |
if [ "$(wc -c < "$i")" -eq "0" ]; then | |
t="$dir/.sha1twins/0" | |
if [ ! -e "$t" ] || ! grep -q "$i" "$t"; then | |
echo "$i" >> "$t" | |
fi | |
else | |
t=$(sha1sum "$p" | cut -d' ' -f1) | |
j="$dir/.sha1/$t" | |
q="$(realpath "$j")" | |
if [ -e "$j" ] && [ "$q" != "$p" ]; then | |
t="$dir/.sha1twins/$t" | |
if [ ! -e "$t" ]; then | |
basename "$q" > "$t" | |
fi | |
if ! grep -q "$i" "$t"; then | |
echo "$i" >> "$t" | |
fi | |
fi | |
fi | |
ln -sfT "$p" "$j"; | |
fi | |
done | |
) | |
} | |
[ $# -gt 0 ] && fix_lib_links "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment