3. OS이야기
Unix 계열에서 디렉토리네 모든 파일에서 특정문자 치환
OSSW(Open Source System SoftWare
2010. 3. 24. 11:43
현재 디레토리 하부구조에서 특정 문자열을 포함하고 있는 모든 파일 목록 찾기
find . -type f | xargs grep -l web_test > /tmp/t.txt
목록파일을 읽어들여 목록에 나열된 각 파일에서 특정 문자열을 새로운 문자열로
치환하는 명령어
sh rep.sh /tmp/t.txt web_test testibm01 나열된 파일에서 web_test를 testibm01로 변경
rep.sh 내용
#!/bin/sh
if [ $# != 3 ];then
echo "Uages: /tmp/rep.sh <filelist> <old_str> <new_str>"
exit;
fi
set -x
mkdir -p /tmp/tt
for file in `cat $1`
do
echo ""
echo $file
cat $file | sed -e "s/$2/$3/g" > /tmp/tt/temp.txt
mv /tmp/tt/temp.txt $file
done
set +x