在Ubuntu中防止文件被误删除,可以采取以下几种方法:
.bashrc 文件重定义 rm 命令rm 命令删除的文件。rm 指令:alias rm='trash'
alias sudorm='sudotrash'
alias rl='ls /.trash'
alias urundelfile='undelfile'
alias undelfile() { mv -i /.trash/$1 / }
alias trash() { mv "$1" /.trash/ }
alias sudotrash() { sudo mv "$1" /.trash/ }
alias cleartrash() {
read -p "Clear sure? [y/n]"
if [ "$REPLY" == "y" ]; then
rm -rf /.trash/*
fi
}
source ~/.bashrc 使重定义生效。trash-cli 命令行工具trash-cli:cd /tmp
git clone https://github.com/andreafrancia/trash-cli.git
cd trash-cli
sudo python setup.py install
trash-cli:trash /path/to/file # 将文件移动到回收站
ls ~/.local/share/Trash/files # 查看回收站中的文件
restore ~/.local/share/Trash/files/file_name # 恢复文件
extundelete:sudo apt-get install extundelete
extundelete 恢复文件。chmod 和 chown 命令设置文件权限。lsof 监控打开文件lsof:sudo apt-get install lsof
lsof:lsof | grep deleted # 查看被删除但仍被进程使用的文件
inotifywait 监控文件系统事件inotify-tools:sudo apt-get install inotify-tools
inotifywait:inotifywait -m /path/to/directory -e delete |
while read path action file; do
echo "File $file was deleted from $path"
# 可以在这里添加其他操作,比如发送通知
done
通过上述方法,可以有效防止在Ubuntu中误删文件。如果问题复杂或数据非常重要,可能需要寻求专业的数据恢复服务。