Re: pattern matching rename...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Todd Zullinger wrote:
...
There are other rename programs that do work this way.  It's annoying
if you switch between such systems and find out that rename doesn't
behave like you're expecting. :)

Which is a good reason to not depend on it.

I've learned to type up something like this:

$ for f in *.{JP*G,jpeg};do n=`echo "$f"| \
sed -e "s/\.jpe*g/.jpg/i"`; echo mv "$f" "$n"; done

easily enough that I don't miss something like 'rename' too much. I can easily tailor the command to the need of the moment (e.g. substitute tr for sed) and I can easily see the results before I do anything horrible (note the 'echo mv').

For a rename command you can easily take with you, here's one that allows regular expressions, and comes complete with diagnostics and error checking ;-)

# usage: rename from to files...
#
function rename () {
    local from="$1"; shift
    local to="$1"; shift
    local n=0
    local f

    for f in "$@"
    do
      local new=`echo "$f" | sed -e "s$from$tog"`

      # some problem with regexp: give up
      test $? = 0 -a "$new" || break

      if [ "$f" != "$new" ]
      then
	  if [ -f "$new" ]
	  then
	      echo "file '$new' exists: skipped" >&2
	  else
	      echo mv "$f" "$new" && n=$(($n+1))
	  fi
      fi
    done
    echo "renamed $n files" >&2
}

NOTE: The safety is 'on' here too. Change 'echo mv' to 'mv' if you want to use it, but don't say I didn't warn you when it eats your homework and kicks your dog.

NOTE ALSO: The sed command uses Ctrl-V as a separator character, just to avoid having to quote the separator in the $from pattern. It may get lost or mangled in the mail.

Add to your ~/.bashrc and season to taste.

<Joe


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux