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

Re: how to rename multiple files

drew cohan wrote:

> How do I rename all files in a directory matching the pattern *.JPG to
> *.jpg in a bash shell script?  Thanks to you guys I can check for the
> existence of jpgs in a directory, but can't seem get 'mv' to rename them
> for me (always complains that the last argument must be a directory).

That's because the shell expands filenames. If you have the following files
in the current directory:


then the command

   mv *.JPG *.jpg

turns into

   mv 1.JPG 2.JPG 3.JPG *.jpg

and so mv complains that if you're moving multiple files, the target
must be a directory (which makes sense). (The *.jpg is not expanded
because there are, I assume, no files ending with .jpg in the

What you really want is something like this:

   for f in *.JPG; do mv $f `basename $f .JPG`.jpg; done

which means "for each .JPG file, move it to the same name, but with the
.JPG ending stripped off, and .jpg appended". Thus 1.JPG becomes 1.jpg,

As long as your .JPG filenames don't have spaces or other shell-unfriendly
characters in them, this should do the job.


Reply to: