Find Command – Prepend to Filename with Find Command

command linecoreutilsfind

Is there a way with only the find command to prepend a string to filenames?

So that this:

201a.txt
201b.png
...

Becomes this:

foo_201a.txt
foo_201b.png
...

This:

find . -name '201*' -execdir mv {} foo_{} \;

Does not work because the {} part includes the leading ./ in the filename, and therefore tries to write to foo_./201*.

If this is not possible with only find, what is the most portable (read: only coreutils, no shell scripts, easiest to understand) way to prepend a string to filenames?

Best Answer

No. But the rename command offers a simple solution.

$ ls -1
201a.txt
201b.png
$ rename 201 foo_201 201*
$ ls -1
foo_201a.txt
foo_201b.png
$