[Wlug] Bash help

Skip Gaede sgaede@attbi.com
Thu, 18 Jul 2002 07:30:13 -0400


On Wednesday 17 July 2002 23:47, Tim Trachimowicz wrote:
> Hey folks!
>
> I have a bunch of files on a server that are in the
> format "filename_x.dat" and I need to rename them
> "filename.dat", i.e. strip to last two characters off the
> filename while maintaining the extension.
>
Here's yet another way of doing it, by using bash builtins:

for _file in $(ls *_x.*); do
  _front=${_file%\_*}
  _back=${_file#*\.}
  mv $_file $_front.$_back
done;

The code may need to be tweaked slightly to deal with 
multiple underscores or multiple dots in the filename, and 
if they all end in .dat, then the logic is even simpler.

--Skip