lum*_*ric 6 filesystem find encoding
我遇到了此问答中描述的问题。可能来自相当老的 linux 发行版或来自 windows 我有几个文件名损坏的文件。ls
显示“?” 而不是破碎的字符。我成功地重命名了其中的一些文件,但我不知道我是否找到了所有这些文件。
有没有办法找到所有受影响的文件?
gei*_*rha 11
假设您使用的是 utf-8 编码(Ubuntu 中的默认编码),这个脚本应该可以识别文件名并为您重命名。
它的工作原理是使用带有 C 编码 (ascii) 的 find 来定位其中包含不可打印字符的文件。然后它会尝试确定这些不可打印的字符是否为 utf-8 字符。如果不是,它会向您显示使用enc
数组中列出的每种编码解码的文件名,允许您选择看起来正确的文件名以对其进行重命名。
latin1 通常用于较旧的 linux 系统,而 windows-1252 现在通常被 windows 使用(我认为)。iconv -l
将向您显示可能的编码列表。
#!/bin/bash
# list of encodings to try. (max 10)
enc=( latin1 windows-1252 )
while IFS= read -rd '' file <&3; do
base=${file##*/} dir=${file%/*}
# if converting from utf8 to utf8 succeeds, we'll assume the filename is ok.
iconv -f utf8 <<< "$base" >/dev/null 2>&1 && continue
# display the filename converted from each enc to utf8
printf 'In %s:\n' "$dir/"
for i in "${!enc[@]}"; do
name=$(iconv -f "${enc[i]}" <<< "$base")
printf '%2d - %-12s: %s\n' "$i" "${enc[i]}" "$name"
done
printf ' s - Skip\n'
while true; do
read -p "? " -n1 ans
printf '\n'
if [[ $ans = [0-9] && ${enc[ans]} ]]; then
name=$(iconv -f "${enc[ans]}" <<< "$base")
mv -iv "$file" "$dir/$name"
break
elif [[ $ans = [Ss] ]]; then
break
fi
done
done 3< <(LC_ALL=C find . -depth -name "*[![:print:][:space:]]*" -print0)
Run Code Online (Sandbox Code Playgroud)