Linux 檔案系統中,亂碼檔名(如編碼錯誤、特殊字元)無法直接用 rm 指定。利用 inode 編號可精準定位並刪除,避免誤刪其他檔案。
[root@localhost tmp]# ls -il
total 1024
917852 -r--r--r-- 1 root root 624541 May 3 09:33 mkisofs-2.01-10.7.el5.x86_64.rpm
919797 srwxrwxrwx 1 mysql mysql 0 Jul 2 12:26 mysql.sock
920533 -rw-r--r-- 1 root root 0 Jul 27 08:27 亂碼file
918587 -rw-r--r-- 1 root root 0 Jul 27 08:26 嘎兒
-i 參數顯示 inode 編號(第一欄)亂碼file 的 inode 為 920533find 依 inode 定位並刪除:# 先確認找到正確檔案
find . -inum 920533
# 輸出:./亂碼file
# 執行刪除
find . -inum 920533 -exec rm -f {} \;
[root@localhost tmp]# ls -l
total 1024
-r--r--r-- 1 root root 624541 May 3 09:33 mkisofs-2.01-10.7.el5.x86_64.rpm
srwxrwxrwx 1 mysql mysql 0 Jul 2 12:26 mysql.sock
-rw-r--r-- 1 root root 0 Jul 27 08:26 嘎兒
亂碼檔案已移除,正常中文檔名檔案保留。
# 列出所有無法正常顯示檔名的檔案 inode
find . -maxdepth 1 -type f -printf "%i %f\n" | while read inode name; do
# 判斷檔名是否包含非法字元(簡易判斷)
if [[ ! "$name" =~ ^[[:print:]]+$ ]]; then
echo "刪除 inode $inode: $name"
find . -inum "$inode" -exec rm -f {} \;
fi
done
ls -i、stat、find -inum 皆可查看 inode。