Skip to content

Instantly share code, notes, and snippets.

@githubhjs
Last active December 5, 2025 08:17
Show Gist options
  • Select an option

  • Save githubhjs/52101040aaeabbce0fcc1752ab29b6be to your computer and use it in GitHub Desktop.

Select an option

Save githubhjs/52101040aaeabbce0fcc1752ab29b6be to your computer and use it in GitHub Desktop.
(繁體中文) 你的 NFS .nfsXXXX 殘留檔案是經典痛點。因為有 process 還 hold 著 FD,所以 rm 會直接吐你一句 device or resource busy。最好的策略,就是自動 fallback → 改用 mv 把東西丟去一個「墓地目錄」,並附上 UUID,不阻塞你的 flow。 下面給你一個可直接放在 $PATH 裡的 rm-wrapper.sh。設計理念是: 先試著正常 rm -rf。 如果遇到 device or resource busy → 自動改名成 .deleted-by-moving.<UUID>。 顯示訊息方便 debug。
#!/usr/bin/env bash
set -e
if [ $# -eq 0 ]; then
echo "Usage: rm-wrapper.sh <target1> <target2> ..."
exit 1
fi
for target in "$@"; do
echo "Processing: $target"
# Try removing normally
rm -rf "$target" 2>/tmp/rm.err && {
echo " Removed successfully."
continue
}
# Check for NFS busy error
if grep -q "Device or resource busy" /tmp/rm.err; then
uuid=$(uuidgen)
base=$(basename "$target")
newname=".deleted-by-moving.$base.$uuid"
echo " Busy detected → renaming to $newname"
mv "$target" "$newname" 2>/dev/null || {
echo " mv failed: still locked. Manual cleanup needed."
}
else
echo " rm failed for a different reason:"
cat /tmp/rm.err
fi
done
rm -f /tmp/rm.err
@githubhjs
Copy link
Copy Markdown
Author

升級版,完整支援多個參數與 wildcard expansion,行為與 rm -rf 幾乎一致,只是多了 NFS busy fallback。

⭐ 特點

可以吃多個檔案:

rm-wrapper.sh a b c d/*.log

每個檔案單獨處理,不會互相影響。

如果遇到 Device or resource busy 就 fallback → mv .deleted-by-moving.。

其他 error 會列出給你看,但不中斷後續檔案處理。

這版已經夠穩、可直接 alias 成你的 rm。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment