Skip to content

Instantly share code, notes, and snippets.

@branquito
Created March 1, 2013 00:19
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save branquito/5061382 to your computer and use it in GitHub Desktop.
Save branquito/5061382 to your computer and use it in GitHub Desktop.
batch rename files or folders recursively in bash
command to find recursively files {can be modified for folders too, change -type f}, and renamed them by specfying regex pattern
find . -type f -maxdepth [depth] -name "[filepattern]" | while read FNAME; do mv "$FNAME" "${FNAME//search/replace}"; done
example:
find . -type f -maxdepth 1 -name "domain*.php" | while read FNAME; do mv "$FNAME" "${FNAME//domain/lead}"; done
before: after:
domains.php leads.php
domain.php lead.php
to do same for folders just change -type f --> -type d
@joselcvarela
Copy link

Thank you, very helpful :D

@larmoe
Copy link

larmoe commented Dec 17, 2019

Thanks. Saved me lot of trouble!

@branquito
Copy link
Author

@larmoe you're welcome, glad it helped

@branquito
Copy link
Author

@joselcvarela you're welcome!

@kentarofujiy
Copy link

cheers mate!

@fabriciocybershell
Copy link

fabriciocybershell commented Sep 28, 2020

the find command argument is reversed, it does not accept global commands before the other parameters, correction: find. -maxdepth [depth] -type f -name "[filepattern]" ...
it may be a question of some version or the options selected in [depth]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment