Skip to content

Instantly share code, notes, and snippets.

@wteuber
Last active March 29, 2018 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wteuber/5336146 to your computer and use it in GitHub Desktop.
Save wteuber/5336146 to your computer and use it in GitHub Desktop.
bulk rename: Replace all files, directories and occurrences in files of an old name with a new name
# HINT: This gists uses GNU commands. On Mac OS, you might want to adopt the script or use `grep`, `find` and `sed` "--with-default-names".
RENAME_FROM=old_name
RENAME_TO=new_name
# RENAME files, directories from an old name to a new name
# CHECK files manually: find . -type f -name "*$RENAME_FROM*" | grep -v '/\.'
# find . -type f -name "*$RENAME_FROM*" ## find all files that contain the old name
# grep -v '/\.' ## exclude hidden files
# ruby -e 'puts ARGF.sort{|a,b| b.length<=>a.length}' ## sort file paths from longest to shortest
# while read filename; do <...>; done ## execute <...> with filename set to the current line
# mv "${filename}" "<...>" ## rename the file from ${filename} to <...>
# `echo "${filename}" | sed -e "s/\(.*\)$RENAME_FROM/\1$RENAME_TO/"` ## replace old file name (RENAME_FROM) with new file name (RENAME_TO) using
# RENAME files
find . -type f -name "*$RENAME_FROM*" | grep -v '/\.' | ruby -e 'puts ARGF.sort{|a,b| b.length<=>a.length}' | while read filename; do mv "${filename}" "`echo "${filename}" | sed -e "s/\(.*\)$RENAME_FROM/\1$RENAME_TO/"`"; done
# CHECK directories manually: find . -type d -name "*$RENAME_FROM*" | grep -v '/\.'
# find . -type d -name "*$RENAME_FROM*" ## find all directories that contain the old name
# grep -v '/\.' ## exclude hidden directories
# ruby -e 'puts ARGF.sort{|a,b| b.length<=>a.length}' ## sort directory paths from longest to shortest
# while read dirname; do <...>; done ## execute <...> with directory name set to the current line
# mv "${dirname}" "<...>" ## rename the directory from ${filename} to <...>
# `echo "${dirname}" | sed -e "s/\(.*\)$RENAME_FROM/\1$RENAME_TO/"` ## replace old directory name (RENAME_FROM) with new directory name (RENAME_TO) using
# RENAME directories
find . -type d -name "*$RENAME_FROM*" | grep -v '/\.' | ruby -e 'puts ARGF.sort{|a,b| b.length<=>a.length}' | while read dirname; do mv "${dirname}" "`echo "${dirname}" | sed -e "s/\(.*\)$RENAME_FROM/\1$RENAME_TO/"`"; done
# REPLACE occurences in files of an old name with a new name
# CHECK occurences manually: grep -l -R -I --exclude "*.log" -e "$RENAME_FROM" .
# grep -l -R -I --exclude "*.log" -e "$RENAME_FROM" . ## list files that contain the old name, excluding .log files
# grep -v '/\.' ## don't list dot files
# xargs sed -i "s/$RENAME_FROM/$RENAME_TO/g" ## replace occurence of old name (RENAME_FROM) with the new name (RENAME_FROM) in those files
grep -l -R -I --exclude "*.log" -e "$RENAME_FROM" . | grep -v '/\.' | xargs sed -i "s/$RENAME_FROM/$RENAME_TO/g"
@wteuber
Copy link
Author

wteuber commented Oct 26, 2016

git ls-files | grep "[pP]erson_\?[hH]olding_\?[aA]ccount_\?[nN]umber"
find . -type f -name "*person_holding_account_number*" | grep -v '/\.' | awk '{ print length($0),$0 | "sort -nr"}' | awk '{$1=""; print $2}' | while read filename; do mv "${filename}" "`echo "${filename}" | sed -e "s/\(.*\)person_holding_account_number/\1aux_reference/"`"; done
find . -type d -name "*person_holding_account_number*" | grep -v '/\.' | awk '{ print length($0),$0 | "sort -nr"}' | awk '{$1=""; print $2}' | while read filename; do mv "${filename}" "`echo "${filename}" | sed -e "s/\(.*\)person_holding_account_number/\1aux_reference/"`"; done

git grep "[pP]erson_\?[hH]olding_\?[aA]ccount_\?[nN]umber" | grep -oue "[pP]erson_\?[hH]olding_\?[aA]ccount_\?[nN]umber" | sort | uniq
grep -l -R -I --exclude "*.log" -e "PersonHoldingAccountNumber" . | grep -v '/\.' | xargs sed -i "" "s/PersonHoldingAccountNumber/AuxReference/g"
grep -l -R -I --exclude "*.log" -e "personHoldingAccountNumber" . | grep -v '/\.' | xargs sed -i "" "s/personHoldingAccountNumber/auxReference/g"
grep -l -R -I --exclude "*.log" -e "person_holding_account_number" . | grep -v '/\.' | xargs sed -i "" "s/person_holding_account_number/aux_reference/g"

@wteuber
Copy link
Author

wteuber commented Apr 7, 2017

StableYamlFormatter --> YamlNormalizer

$ sed --version
sed (GNU sed) 4.4
git ls-files | grep "[sS]table[_ ]\?[yY]aml[_ ]\?[fF]ormatter"

find . -type f -name "*stable_yaml_formatter*" | grep -v '/\.' | awk '{ print length($0),$0 | "sort -nr"}' | awk '{$1=""; print $2}' | while read filename; do mv "${filename}" "`echo "${filename}" | sed -e "s/\(.*\)stable_yaml_formatter/\1yaml_normalizer/"`"; done
find . -type d -name "*stable_yaml_formatter*" | grep -v '/\.' | awk '{ print length($0),$0 | "sort -nr"}' | awk '{$1=""; print $2}' | while read filename; do mv "${filename}" "`echo "${filename}" | sed -e "s/\(.*\)stable_yaml_formatter/\1yaml_normalizer/"`"; done

git grep "[sS]table[_ ]\?[yY]aml[_ ]\?[fF]ormatter" | grep -oue "[sS]table[_ ]\?[yY]aml[_ ]\?[fF]ormatter" | sort | uniq

grep -l -R -I --exclude "*.log" -e "Stable Yaml Formatter" . | grep -v '/\.' | xargs sed -i "s/Stable Yaml Formatter/Yaml Normalizer/g"
grep -l -R -I --exclude "*.log" -e "StableYamlFormatter" . | grep -v '/\.' | xargs sed -i "s/StableYamlFormatter/YamlNormalizer/g"
grep -l -R -I --exclude "*.log" -e "stable_yaml_formatter" . | grep -v '/\.' | xargs sed -i "s/stable_yaml_formatter/yaml_normalizer/g"

@wteuber
Copy link
Author

wteuber commented Mar 29, 2018

Rename Translator to I18n Yaml Editor:

# Check files and directories to be renamed
git ls-files | grep "[tT]ranslator"
find . -name "*translator*"

# Rename files and directories
find . -type f -name "*translator*" | grep -v '/\.' | awk '{ print length($0),$0 | "sort -nr"}' | awk '{$1=""; print $2}' | while read filename; do mv "${filename}" "`echo "${filename}" | sed -e "s/\(.*\)translator/\1i18n_yaml_editor/"`"; done
find . -type d -name "*translator*" | grep -v '/\.' | awk '{ print length($0),$0 | "sort -nr"}' | awk '{$1=""; print $2}' | while read filename; do mv "${filename}" "`echo "${filename}" | sed -e "s/\(.*\)translator/\1i18n_yaml_editor/"`"; done

# Check occurences in files to be renamed
git grep "[tT]ranslator"
git grep "[tT]ranslator" | grep -oue "[tT]ranslator" | sort | uniq

# Rename occurences in files
grep -l -R -I --exclude "*.log" -e "translator" . | grep -v '/\.' | xargs sed -i "s/translator/i18n_yaml_editor/g"
grep -l -R -I --exclude "*.log" -e "Translator" . | grep -v '/\.' | xargs sed -i "s/Translator/I18n Yaml Editor/g"
# Fix occurences in code
grep -l -R -I --exclude "*.log" -e "I18n Yaml Editor::" . | grep -v '/\.' | xargs sed -i "s/I18n Yaml Editor::/I18nYamlEditor::/g"
grep -l -R -I --exclude "*.log" -e "I18n Yaml Editor.app" . | grep -v '/\.' | xargs sed -i "s/I18n Yaml Editor.app/I18nYamlEditor.app/g"
grep -l -R -I --exclude "*.log" -e "include I18n Yaml Editor" . | grep -v '/\.' | xargs sed -i "s/include I18n Yaml Editor/include I18nYamlEditor/g"
grep -l -R -I --exclude "*.log" -e "module I18n Yaml Editor" . | grep -v '/\.' | xargs sed -i "s/module I18n Yaml Editor/module I18nYamlEditor/g"

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