One of possibly solutions:
find . -name '*.jpg' -printf "'%p' '%h/thumb_%f'\n" | xargs -n2 echo mv
Principe: find all needed files, and prepare arguments for the standard mv
command.
Notes:
- arguments for the
mv
are surrounded by '
for allowing spaces in filenames.
- The drawback is: this will not works with filenames what are containing
'
apostrophe itself, like many mp3 files. If you need moving more strange filenames check bellow.
- the above command is for dry run (only shows the mv commands with args). For real work remove the
echo
pretending mv
.
ANY filename renaming. In the shell you need a delimiter. The problem is, than the filename (stored in a shell variable) usually can contain the delimiter itself, so:
mv $file $newfile #will fail, if the filename contains space, TAB or newline
mv "$file" "$newfile" #will fail, if the any of the filenames contains "
the correct solution are either:
- prepare a filename with a proper escaping
- use a scripting language what easuly understands ANY filename
Preparing the correct escaping in bash is possible with it's internal printf
and %q
formatting directive = print quoted
. But this solution is long and boring.
IMHO, the easiest way is using perl
and zero padded print0
, like next.
find . -name \*.jpg -print0 | perl -MFile::Basename -0nle 'rename $_, dirname($_)."/thumb_".basename($_)'
The above using perl's power to mungle the filenames and finally renames the files.