use*_*361 3 shell shell-script rm
我想创建一个脚本,每当删除三个以上的文件时,它都会rm -i默认调用。我知道它会进入初始化文件,但似乎无法让它工作。
迄今为止:
if [$file -ge 3]; then
rm -i
exit 0
else
rm
exit 1Run Code Online (Sandbox Code Playgroud)
ter*_*don 15
@Tim 的回答中给出的函数适用于任何运行 bash 的系统。但是,GNUrm已经为您提供了所需的选项(来自man rm):
-I prompt once before removing more than three
files, or when removing recursively. Less
intrusive than -i, while still giving protec?
tion against most mistakes
Run Code Online (Sandbox Code Playgroud)
因此,如果您有 GNU rm(如果您运行的是 Linux,您应该这样做),您只需要将此行添加到您的/.bashrc:
alias rm='rm -I'
Run Code Online (Sandbox Code Playgroud)
删除超过 3 个文件前会提示,但不会对每个文件都要求确认,如果您确认该操作,将删除所有文件。
这对我有用,将这些行添加到您的~/.bashrc:
rm() {
if [ "$#" -ge 3 ]; then
command rm -i "$@"
else
command rm "$@"
fi
}
Run Code Online (Sandbox Code Playgroud)
这将创建一个rm函数,而不是/bin/rm每次执行时都会调用该函数rm。
"$#" 扩展为传递的参数数量。