Renaming Files on Hoary

Tony Arnold tony.arnold at
Tue Jun 14 22:19:10 UTC 2005


On Tue, 2005-06-14 at 17:24 -0400, Stephen R Laniel wrote:
> On Tue, Jun 14, 2005 at 10:09:30PM +0100, Tony Arnold wrote:
> > find . -name '*.JPG' -exec mv {} `cat {} | sed -e 's/\.JPG/.jpg/'` \;
> I think you meant 'echo' where you wrote 'cat' above.

Oops! Yes, I did. I thought it didn;t look quite right when I wrote it!

> I'd simplify your script a little. First, the '-e' in sed is
> superfluous. Second, bash contains a nice little feature to
> take care of your 'sed' bit above; instead of
> mv $i `echo $i |sed 's/foo/bar'`
> you can do
> mv $i ${i//foo/bar}

That's a new one on me. Mind you bash is full of features that most of
us never go anywhere near!

> So I'd rewrite what you did as
> for i in $(find . -name '*.JPG'); do
> 	mv $i ${i//.JPG/.jpg}
> done
> I like the '$(...)' syntax, because sometimes -- not often,
> but sometimes -- I have embedded backticks, and I don't like
> having to escape them; I think
> $(cmd1 --arg1 $(cmd2) --arg2 $(cmd3))
> looks better than
> `cmd1 --arg1 \`cmd2\` --arg2 \`cmd3\``
> But TMTOWTDI. Your method would work in more shells; this
> one's tuned to bash.

Another bash nicety I've never used. Thanks.

I think I learnt my version of the command on a SunOS 4 system many
years ago before we had bash. Hence my comment about being old

I was always amused to compare the verbosity of the above solution to
how it's done on OpenVMS, which is often accused of being verbose. The
equivalent would be:

	rename [...]*.xxx .yyy

But this is way off topic now.

Tony Arnold, IT Security Coordinator, University of Manchester,
IT Services Division, Kilburn Building, Oxford Road, Manchester M13 9PL.
T: +44 (0)161 275 6093, F: +44 (0)870 136 1004, M: +44 (0)773 330 0039
E: tony.arnold at, H:

More information about the ubuntu-users mailing list