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

Re: the mv trap

On Mon, Aug 13, 2007 at 04:34:52PM +0200, Vincent Lefevre wrote:
> On 2007-08-13 07:44:39 +0000, zzapper wrote:
> > red face; in fact my mv does report "not a directory".
> > My problem is reduced to unintentionally moving a single file to another 
> > single file rather than the intended directory.
> > 
> > MM solves this by suggesting a trailing slash
> > 
> > $ mv x3 bucket/
> > mv: accessing `bucket/': Not a directory
> If you fear to forget the trailing slash, you can also write a shell
> script/function that checks that the target is a directory, then
> executes mv.

The thing is you sometimes do want to rename files with mv. The
wrapper function could be made so as to check if there doesn't
exist a directory spelled like the last argument.

Something like:

mv() {
  local dest=$@[-1]
  if [[ ! -d $dest ]]; then
    local -a files
    setopt localoptions extendedglob
    if (($#files)); then
      printf >&2 '"%s" doesn'\''t exist as a directory but\n' ${(V)dest}
      printf >&2 'there exist %d directory(ies) with a close name:\n' $#files
      local i
      for ((i = 1; i <= $#files; i++)); do
        printf >&2 ' %d "%s"\n' "$i" ${(V)files[i]}
      printf >&2 'Would you like to move files to one of those instead (n12.)? [n] '
      local answer
      read answer
      if [[ $answer = [0-9]## ]]; then
	set -x
  command mv "$@"



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