I'm just trying to add a confirmation prompt to a few linux commands like chmod and chown.

I've tried to google this but I can't find much information on the topic.

While searching I found out that there is a solution for rm using the -i option, though that can be overwritten with -f

However there is safe-rm package which you can install on your server which blacklists certain important directories which is quite a good solution, but sadly there is no similar package for chmod and chown.

So my ideal solution would be where chmod and chown have confirmation prompts and also certain directories are blacklisted from being chmod-ed and chown-ed.

Any answers much appreciated !!!

  • chmod and chown don't have confirmation switches because their effect are always reversible by root. There's no point in asking confirmation for an action that's reversible. – Larssend Jul 14 '15 at 17:09
  • it's reversible but you can chown or chmod / and then if you don't know the exact permission, as there are quite a few files in there, you are lost – Petar Vasilev Jul 14 '15 at 17:12
  • But still, the data wouldn't be lost, so it's still not as dangerous. – Larssend Jul 14 '15 at 17:13
  • True but regardless of the data if the permissions and ownerships are not right many things on the server will be broken and repairing them isn't necessarily easy – Petar Vasilev Jul 14 '15 at 17:18
  • 1
    Repairing them consists of either restoring a backup or using a backup to get a list of the permissions as they should be. And how to do that has been asked before. – Hennes Jul 14 '15 at 17:37

The commands chown (1) and chmod (1) do not natively have sunch an option. If you want to add that functionality then you either:

  1. Have to change the source and recompile.
  2. Or write a wrapper.

The first option is the cleanest. The second the easier. If you get stuck writing that then you need a new question which is likely a better fit for [SO], but briefly:

1) move chmod somewhere else. e.g. mv /bin/chmod /bin/chmold.therealthing

2) Create an executable script called chmod which does what you want (and which calls the real chmod).

Untested something from a non-shell script person (aka me:)

#!/usr/bin/env bash
echo "/bin/chmod (Shell script) called with these arguments:"
echo $@

read -p "Are you sure you want to do this? (y/n) " RESPONSE
if [ "$RESPONSE" = "y" ]; then
    exec /bin/chmod.therealthing $@        
    echo "OK, aborting."

if you want more granular control then the script rapidly becomes more complex. Stiill, of you wish you could parse the input and execute a chmod for every file (after checking for a valid syntax).

  • Cheers for that, it seems like it's going to work : ), btw you've missed a double quote on the second line, also I'm trying to make this into a bash function but when I call it closes my shell, any chance you know why it's doing that? – Petar Vasilev Jul 16 '15 at 16:29
  • Does it close only when you accept? I have an exec in there. (probably from my days when RAM was scarce). – Hennes Jul 16 '15 at 17:18
  • Yep after accepting I just removed the exec actually and it sopped doing that : ) – Petar Vasilev Jul 17 '15 at 8:07

A solution in bash would be to use shell functions and aliases. You could put them in /etc/bashrc or ~/.bashrc if you only want them for specific users. Here's some function and alias definitions you could use to do something like what you asked.


_dangerouspath() {
        file=$(readlink -e "$1")
        [ -z "${file}" ] && return 1

        while read pattern
                [[ "${file}" =~ ^${pattern}$ ]] && return 0
        done < ${DANGERLIST}

        return 1

_checkpaths() {
        while [ -n "$1" ]
                [[ "X$1" =~ ^X[^-].* ]] && _dangerouspath $1 && return 0

        return 1

_saferun() {
        if _checkpaths $*
                read -p "Are you sure you want to do this? (y/n) " r && [ "$r" = "y" ]  && $p $*
                $p $*

alias chown="_saferun /bin/chown"
alias chgrp="_saferun /bin/chgrp"
alias chmod="_saferun /bin/chmod"

You'd need to create a list of paths you want to protect in "$DANGERLIST" (/etc/dangerous.paths) which would be something like

  • This seems like it should work : ) – Petar Vasilev Jul 17 '15 at 13:48

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.