Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: add an mv protection ?



Marc Chantreux wrote:
> % echo *
> a.pl b.pl
> % mkdir old
> % mv *pl
> 
> Here, i forget the target ( old ). The result is :
> 
> mv a.pl b.pl
> 
> i've lost the b.pl content!
> 
> is there a way to hook this case and ask a confirmation ?

Well, you can use a wrapper function:

mv() {
    if (( $# == 1 )); then
        print "mv: only one argument, did you mean that?"
        return 1
    fi
    local -a args
    args=(${~*})
    print command mv $args
}
# careful to do this after the above or you create a function "noglob"
alias mv='noglob mv'

but this isn't quite good enough, since it doesn't handle options,
for example "mv -f *.pl" or "mv -i *.pl".  An alternative approach would
be to check whether the final argument had wildcards in it, replacing
the "if" with:

   if [[ $argv[-1] = *["*?[]()#"]* ]]; then
        print "mv: last argument contains wildcards, aborted"
	return 1
   fi

but that's still not good enough if the destination might have quoted
wildcards in it.  I suppose you could use:

    local -a lastarg
    lastarg=(${~argv[-1]})
    if (( ${#lastarg} > 1 )); then
        print "mv: last argument generates multiple files, aborted"
        return 1
    fi

although that might still be imperfect.

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php



Messages sorted by: Reverse Date, Date, Thread, Author