有一些终端命令可以非常有效地破坏大量数据。例如:sudo rm -rf /*
或rm -rf /*
。我想设置一个警告,只要输入这些破坏性命令就会显示警告。如下所示:
sudo rm -rf /*
Are you sure you want to remove all files from your root
directory recursively? This operation will remove all files from the root
directory, any mounted filesystems attached to it and essential operating
system files.
Are you sure you want to proceed? [Y/n]
rm -rf /*
Are you sure you want to remove all files owned by $USER
recursively? This operation will remove all your files on the root
filesystem and any filesystems mounted to it.
Are you sure you want to proceed? [Y/n]
如何编写一个脚本来实现这个功能?
答案1
rm
-i
每次删除文件前都会询问。我认为这不是你想要的,因为它会询问每一个删除文件之前的时间,如果您想递归删除,例如 git repo,通常会进入数百次确认。
你想要的可能是一个简单的脚本,可以rm
像这样“替换”
#!/bin/bash
if [ "$(ls -l $1 | wc -l)" = "1" ]; then
rm $1 $@
else
echo "You are going to delete these ($(ls -l $1 | wc -l)) files/directories via shell globbing"
ls $1
read -p "Do you really want to delete these files? [y/n]" yn
if [ $yn = [Yy] ]; then
rm $1 $@
fi
fi
注意:您必须使用类似“rm FILE ARGUMENTS”的脚本。
如果您使用 shell 通配符选择多个文件(目录),此脚本将会查找,但如果只有一个文件,则会删除该文件。