Linux people: Can you help me rename a bunch of files?

Suppose that I have a directory listing that looks like this:

123CAT.sql
456CAT.sql
789CAT.sql
XYZ.SQL

I would like to be able to run a script that replaces every instance of CAT with DOG in the filename only so that my directory now shows

123DOG.sql
456DOG.sql
789DOG.sql
XYZ.sql

It only needs to work for the current directory. It doesn’t need to be recursive.

Any ideas? I’m currently using winscp and renaming them manually but there’s got to be an easier way.

In a Linux (bash) shell:


for file in *CAT*; do
    mv $file ${file/CAT/DOG}
done

No need for a bash script, just run :

rename ‘s/CAT./DOG./’ *.sql

in the folder. rename should live in /usr/bin/ and should be on you $PATH so you can just run it as is. Run it with a -n flag if you want a test run rather than the real thing.

Of course, that answer depends on your flavour of Linux and whether you have Perl properly installed (that rename is just a symlink to Perl’s prename script)

Thanks guys (?). Both methods worked for me except for the rename -n option. This is Red Hat 7 and the rename man page only lists options for verbose, version, symlink and help.

I appreciate it.

nm

A simple “rename CAT DOG *.sql” worked for me on Red Hat 7.

I’m on CentOS 7 and rename is installed, but it might be part of a package that needs to be installed?