zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <Stephane_Chazelas@yahoo.fr>
To: zsh-workers@sunsite.dk, zsh-users@sunsite.dk
Subject: Re: the mv trap
Date: Mon, 13 Aug 2007 17:50:32 +0100	[thread overview]
Message-ID: <20070813165032.GB5613@sc.homeunix.net> (raw)
In-Reply-To: <20070813143452.GE6199@prunille.vinc17.org>

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
    files=((#a2)$dest(ND/))
    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]}
      done
      printf >&2 'Would you like to move files to one of those instead (n12.)? [n] '
      local answer
      read answer
      if [[ $answer = [0-9]## ]]; then
        argv[-1]=$files[answer]
	set -x
      fi
    fi
  fi
  command mv "$@"
}

        

-- 
Stéphane


      reply	other threads:[~2007-08-13 16:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <Xns998ADF1A94C0Dzzappergmailcom@80.91.229.5>
     [not found] ` <20070813023540.GC6199@prunille.vinc17.org>
2007-08-13  7:44   ` zzapper
2007-08-13 14:34     ` Vincent Lefevre
2007-08-13 16:50       ` Stephane Chazelas [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070813165032.GB5613@sc.homeunix.net \
    --to=stephane_chazelas@yahoo.fr \
    --cc=zsh-users@sunsite.dk \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).