Last active
March 29, 2018 01:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rename Translator to I18n Yaml Editor: