Bash – Batch rename files that end in a dot

bashrename

I have a large unix fileserver, with a single large SMB share. It serves to Mac, Linux and windows clients. A large portion of the files on this box have been migrated there from a Mac Xserve. One of the ways the share is backed up is via a Windows box. That particular backup software fails if there are characters it doesn't like in the filenames.

I've managed to fix most of the filename issues but am struggling with files that end in a dot. For example:

  • filename.
  • newfile.txt.
  • stupid old file.

I am trying to find some way to batch rename all the files that end in a dot to the same name without the dot. So filename. becomes filename

find . -name "*."

works to find the files. I've tried piping it through sed:

find . -name "*." | sed 's/.$//'

which does the job in the console but doesn't actually rename the files. That's the part I'm struggling with.

Best Answer

If you have the Perl script version of rename (called either rename or prename):

rename 's/\.$//' *.

or the util-linux version (called either rename or rename-ul):

 rename . '' *.

If you run the command without arguments and get something similar to:

Usage: rename [-v] [-n] [-f] perlexpr [filenames]

then it's the Perl script version.

Something like:

rename.ul: not enough arguments

Usage: rename.ul [options] expression replacement file...

Options: -v, --verbose explain what is being done -V, --version
output version information and exit -h, --help display this help and exit

means it's the util-linux version.

Related Topic