If I use rm -rfi, this asks me every time each file is deleted. If I have a list of rm -rf commands, how can I make it so that the machine asks me for confirmation just once (when trying to delete the directory itself)
- 66,199
- 35
- 114
- 250
- 171
- 1
- 3
4 Answers
With some rm implementations (DragonFly BSD where it comes from, FreeBSD and GNU at least), the -I (capital i) is what you are looking for.
-i asks for confirmation for every file, while the -I (capital i) ask for confirmation once when files are more than 3 or you are deleting recursively.
- 523
- 3
- 11
zsh -c 'rm -f -- **/*(^/)'
rm -ri -- *
This will recursively (**/) remove everything (*) that isn't a directory (^/) with zsh; then use your existing shell to interactively remove what's left (directories). To include dotfiles in the initial removal, use:
zsh -c 'rm -f -- **/*(D^/)'
followed by:
rm -ri -- *
- 522,931
- 91
- 1,010
- 1,501
- 66,199
- 35
- 114
- 250
The snippet below will ask for confirmation once, then it will delete the entire directory 'path/to/delete'
- Use
read -p 'question ' answerto display the'question 'to the user and store the answer in the variable${answer}. (The trailing space in'question 'makes the prompt more readable) - Use the test clause
[ ]with the string equality=operator to ensure the variable${answer}contains the'expected answer' - Use the and operator
&&to stop if any of the steps does not succeed - e.g.[Ctrl]+[C]was used to interrupt thereador the test clause[ ]wasn't true - Before ever writing
rm -rf- Think of at least 42 scenarios where things can go horribly wrong. Brainstorm ways to mitigate the risk as much as possible - When all of the conditions above are met, remove the directory forcibly with
rm -rf
PATH_TO_DELETE='path/to/delete'
read -p "Delete path '${PATH_TO_DELETE}'? [y/N] " yn && [ "${yn}" = 'y' ] && rm -rf "${PATH_TO_DELETE}"
- 111
- 4
Try this, Well this could be a wild solution, but wanted to share anyway!
function rm_ {
[[ -d "$1" ]] && rm -rI "$1" || rm -f "$1"
}
Then use rm_ <directory|file>. If its directory you get rm: remove all arguments recursively? , else nothing and the file would be deleted.
This way , there will be no prompt to user while deleting files, as well as the user gets a prompt while deleting a directory.
If only rm -rI were to be used , user gets confirmation prompt for both files and directories and so I used a combination of both rm -f and rm -rI.