我想查看一长串被删除的文件,它们不是很重要,所以如果我不小心删除了其中一些文件也没有问题,但我仍然想保存一些文件。
如何拨打电话rm -i ./*
并收到提示rm: remove regular file 'myfile'?
,但将默认值设置为“是”(Y/n)?
答案1
没有什么可以阻止您围绕 来创建自己的层rm
,无论是作为函数还是脚本。 (我个人更喜欢脚本。)
将其复制到文件中yrm
,将其放入您的目录中$PATH
(例如/usr/local/bin
),并使其可执行(chmod a+x /usr/local/bin/yrm
):
#!/bin/sh
#
fails=0 # Number of failures is presented as exit status
for item in "$@"
do
# Skip directories
if [ -d "$item" ]
then
printf '%s: skipping directory\n' "$item" >&2
fails=$((fails+1))
continue
fi
# Prompt the user
printf "%s: remove regular file '%s' (Y/n)? " "${0##*/}" "$item" >&2
read yn || exit $((fails+1))
# Either no response or "y" is good enough for deletion
if [ -z "$yn" ] || [ y = "$yn" ]
then
rm "$item" </dev/null # No -f so we expose error messages
[ $? -gt 0 ] && fails=$((fails+1))
fi
done
# Report failures (0=success)
exit $fails
然后,您可以在不使用任何标志和一个或多个文件的情况下调用它。例如,
yrm *.txt
答案2
您可以使用 case 语句,
# path of file
file_path=$(ls -ltr | grep "^-" | awk -F" " '{ print $9 }' | grep -i ".txt*")
read -p "enter yes for removing files $file_path" user_inp
# -p :- for user prompt
case ${user_inp}
in Yes | yes | YES )
rm $file_path
;;
*)
echo "you don't want to delete that file"
esac