Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Click here to return to the 'One-line batch file renamer' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
One-line batch file renamer
Authored by: loubregand on Jul 28, '04 07:14:29AM

This is my version of the script of Dierdre, which has been improved to handle files with spaces in them and recurse into the subdirectories (find is a very flexible tool for this kind of things):
find . -iname "*pattern1*" -exec echo "mv '{}' '{}'" \; | sed 's/pattern2/pattern3/2' | /bin/sh
pattern1: the mask that select which files to process
pattern2: string to be modified
pattern3: substitution for pattern2 (leave empty for deletion)

Note that, if the script has modified the folder names, you will have to replay the script for every level of recursion, since once it has changed the first level name it will no more find the nested folders.

[ Reply to This | # ]
One-line batch file renamer
Authored by: carmatana on Jan 04, '11 11:35:26PM

Hi loubregand,

Your lines have been the more helpful of all that I tried. I have one question: What if I want to make the rename (sed) just on files, excluding folders or directories at all. I have tried:

find . -type f -iname "*pattern1*" -exec echo "mv '{}' '{}'" \; | sed 's/pattern2/pattern3/2' | /bin/sh

and several more permutations but with no success

something that works is to use for pattern1 somethin like "*pattern1*.*"
and this works as long as the file has an extension, if it does not have one it is not processed

Could you or someone else can help me?

[ Reply to This | # ]