A Transient Mode for Safer Filesystem Operations
I am sure this is standard procedure for many others, but I only discovered this idea now: a temporary safe mode when doing extensive filesystem operations on the (notoriously unforgiving) Unix platform.
The “small, sharp tools” in the Unix toolbox (such as mv and cp)
come with a safety catch: the -i (interactive) flag, which queries
before overwriting an existing file, and the -n (no-clobber) flag,
which silently abandons an operation that would overwrite a file. By
default, these guards are off, and most of the time, that’s exactly
how I want it.
But there is the occasionally scenario when I need to do major filesystem clean-up: moving and renaming lots of files and directories. Then, and only then, would I like the familiar, muscle-memory-acquired commands to exhibit their “interactive” behavior.
The simple and obvious solution is to alias them accordingly, for the particular shell session, only. Once the shell is closed, everything goes back to normal.
In fact, I set an alias in my .tcshrc (yes, TCSH), that also
conveniently changes the shell prompt to remind me. (The %C2
instruction expands to the last two segments of the current working
directory; the - before the > is what distinguishes this prompt
from my normal operations.) Finally, the noclobber shell variable
prevents the redirection operator > from clobbering existing files.
alias safemode 'set prompt="%C2 -> "; alias mv mv -i; alias cp cp -i; set noclobber'
Running safemode at any time during a shell session turns on the
interactive, non-clobbering behavior. There is no need to disable
this mode; I tend to close shells (windows) when I am done with them.