I want to make the command shell to prompt me for confirmation before execution of certain commands like dd , rm -rf etc.

  • 1
    I know it's tagged bash, but in zsh you can get a ten-second wait on rm -rf * using setopt RM_STAR_WAIT or confirmation using setopt NO_RM_STAR_SILENT – UniversallyUniqueID Apr 25 '16 at 17:24

I think safe-rm will do the work

apt install safe-rm

Safe-rm is a safety tool intended to prevent the accidental deletion of important files.

$ rm -rf /usr
Skipping /usr
| improve this answer | |

You can create a bash script that ask confirmation and then run the desidered program. Create a file named ask containing:

echo "Are you sure to run $1 command?"
read RES
if [ "$RES" = "Y" ] || [ "$RES" = "y" ] ; then
    echo "Running the command..."
    echo "Command aborted"

Copy ask script in /usr/bin/ and set it as executable (or add the script path tho PATH variable):

sudo mv path_to_ask_script/ask /usr/bin/ask
cd /usr/bin/
sudo chmod 777 ask

Then you have to create an alias in the ~/.bashrc file:

alias dd='ask dd'

Now by typing dd, the ask script should be executed, ask for confirmation and eventually call the dd command.

To extend to other command just add an alias like:

alias command_name='ask command_name' as example: alias rm='ask rm'

For the rm command you can simply create an alias as: alias rm='rm -i'

Script tried and fully working on 16.04 with gnome-terminal

| improve this answer | |
  • 1
    I think your ask script shouldn't use $1 but $@ cause: ask /bin/cp -Rv /path/to/source /path/to/destination for $1 will return only /bin/cp and $@ will return /bin/cp -Rv /path/to/source /path/to/destination – Dawid Apr 25 '16 at 17:27
  • You're right, My bash ability are a bit rusted – velix Apr 25 '16 at 17:37

You can do the following if you really want to use those commands only as sudo:

Go to /bin(example, most commands are here) so:

cd /bin
# example how to make dd to posibly only for root/sudo (744)
sudo chmod go-x dd
# or even go further restrict the access to the file fully to root (700)
sudo chmod go-rx dd
# to reverse that setting you did do (755)
sudo chmod go+rx dd 

This removes the executable flags from those commands for group and other. You can do that with almost all commands and it till tell you have no permission when not using sudo.

Disclaimer: Do not, I mean it, DO NOT change permissions on sudo or su you might lock you out of essential stuff and wwould have a hard time fixing.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.