Is there a way to prevent deletion of system/shell aliased folders? I want these commands to fail. My shell is bash.
Such as:
$rm -rf .
$rm -rf ..
$rm -rf ~
$rm -rf *
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityNote that ~ and * are expanded by the shell before passing the arguments to rm.
The only way to do it is to define a function named rm that parses the arguments before invoking command rm ...
I would not recommend it. When you get lazy, you get in trouble when you sit at some other computer without your self-protections. I used to alias rm="rm -i" before that bit me.
alias rm="rm -I" is more permissive than -i, I use this. I think these kind of aliases are necessary to prevent these mistakes, its not wise to write commands you know rely on this behaviour though.
/home/glenn, not ~, the list of files, not *)
Jan 11, 2014 at 9:47
Not sure if there is an answer to this, but one thing I do in my .bashrc is to set GLOBIGNORE=.:.., this stops glob expansions from matching . and ... It also effectively enables dotglob which might not be what you want (GLOBIGNORE=.:..:.* stops this). I find this useful for me.
With recent versions of bash:
typetext() {
perl -le 'require "sys/ioctl.ph";
ioctl(STDIN, &TIOCSTI, $_) for split "", join " ", @ARGV' "$@"
}
checkrm() {
local re='\<rm\>.*[[:blank:]]([.*~]|\.\.)([[:blank:]]|$)'
if [[ $READLINE_LINE =~ $re ]]; then
printf > /dev/tty "Are you sure? "
read -n1 k
echo
if [[ $k != [yY] ]]; then
READLINE_LINE=
return
fi
fi
typetext $'\n'
} <> /dev/tty >&0
bind -x '"\C-m": checkrm'
Basically, upon pressing Return, our checkrm function is invoked.
It looks in the currently entered command line ($READLINE_LINE for which you need bash 4.3 or newer) for a rm word (also matches /bin/rm, but not in rmdir) followed by a *, ., ~ or .. argument.
If it matches, it prompts the user for confirmation. If confirmed, a newline character is inserted in tty input buffer for the command to be accepted (both CR and LF are bound to accept line, we're only wrapping CR); if not, the current buffer is emptied.
Note that with versions of bash prior to 4.4, there was a hitting a bug that caused readline to stop functioning properly when a job was suspended.
emacs -nw. Suspend emacs C-z and type: no visible output. Go back to emacs fg and exit emacs. Back at the prompt, whatever you type is invisible.
\<\>, it's a GNU Extension, matching empty string at beginning and end of word, respectively.
bash. Binding an external command to ^M (like bind -x '"\C-m": /bin/true') is enough to trigger it (use Ctrl+J to accept the line as we do in our TIOCSTI ioctl).
Mar 6, 2014 at 20:54
\<\> is more of a BSD extension (was already in ex in the late 70s). It never made it to POSIX basic regular expressions. But has been in GNU regexps for a while if not the beginning. bash is the GNU shell, but I think it uses the system's regex library for its regexps, so it may not work on non-GNU/BSD systems.
Apr 14, 2016 at 8:34
.and..are real links. They are not special shell expansions.tcshandzsh(enabled by default inzsh, you needset rmstarintcsh), do issue a confirmation prompt when you do arm *orrm xxx/*.rmwill refuse to remove.and..(exception iszsh'srmbuiltin (zsh globs never expand.nor..)).rminteractive only when using globbing? (in either bash or zsh or both)