Created
May 19, 2012 02:06
-
-
Save jou4/2728649 to your computer and use it in GitHub Desktop.
対象のディレクトリを再帰的に走査して、ディレクトリ名変更やファイル内の文字列置換を行うスクリプト
This file contains hidden or 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
#!/bin/bash | |
target_dir=${1:-"./"} | |
name_from=src_ | |
name_to=dst_ | |
text_from=old | |
text_to=new | |
avoid_exts="jar|exe" | |
if [ ! -d $target_dir ]; then | |
echo $target_dir is not directory. | |
exit 1 | |
fi | |
# $1: src | |
# $2: from | |
# $3: to | |
function rename(){ | |
local dst=`echo $1 | sed -e "s/$2/$3/"` | |
if [ $1 != $dst ]; then | |
mv $1 $dst | |
echo `pwd`/$1: moved to $dst. | |
return 0 # done | |
fi | |
return 1 | |
} | |
# $1: src | |
# $2: from | |
# $3: to | |
function replace_text(){ | |
sed -i -e "s/$2/$3/g" $1 | |
echo `pwd`/$1: replaced $2 to $3. | |
return 0 # done | |
} | |
# $1: target directory | |
function rec(){ | |
cd $1 | |
{ | |
IFS=$'\n' | |
for line in `ls -a` ; do | |
local child=$line | |
# skip . , .. | |
if [[ $child =~ ^(\\.|\\.\\.)$ ]]; then continue; fi | |
if [ -d $child ]; then | |
rec $child | |
rename $child $name_from $name_to | |
else | |
local ext=.${child##*.} | |
if [[ ! $avoid_exts =~ $ext, ]]; then | |
replace_text $child $text_from $text_to | |
fi | |
fi | |
done | |
} | |
cd ../ | |
} | |
rec $target_dir |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment