[X-Unix] Renaming files with spaces

Philip Robar philip.robar at gmail.com
Fri May 22 16:13:25 PDT 2009


On Thu, May 14, 2009 at 9:26 AM, Charles Howse <chowse at charter.net> wrote:

> Hi,
> I need to batch rename all files with extension .PDF to extension .pdf.
> They may or may not have spaces in the filenames.
>
> The following works ONLY if there are no spaces in the filename:
>
> for f in *.PDF; do
>        base=`basename $f .PDF`
>        mv $f $base.pdf
> done
>
> Can anyone provide a tip to something that will work regardless of whether
> there are spaces in the filenames or not?


Enclosing a variable in double quotes prevents parameter expansion. See the
quoting section of the shell you're using. (Which you failed to mention.)

Using back quotes, ``, for command substitution is deprecated for various
reasons. Use $(), it will make your life simpler.

${base} forces correct parameter expansion, e.g. it states clearly to others
that 'base' is the variable not 'base.pdf'.

for f in *.PDF; do
       base=$(basename "$f" .PDF)
       mv "$f" "${base}".pdf
done

Phil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.themacintoshguy.com/pipermail/x-unix/attachments/20090522/601a4355/attachment.html


More information about the X-Unix mailing list