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]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux