Linux/Unix 작업 중에 특정 단어가 이름에 포함된 파일들을 찾는 목적으로 사용되는 find 명령어가 있습니다. manual은 1000 line을 넘어서 자세히 읽어보긴 어렵지만 아래 help 명령어에서 나오는 기능 중 actions에 있는 부분은 유용하게 쓰일 때가 있습니다. 그중에서 -exec 관련한 기능으로 find로 찾아낸 파일들의 내부에 포함된 문자열을 치환할 수 있습니다.
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:
operators (decreasing precedence; -and is implicit where no others are given):
( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2
EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2
positional options (always true): -daystart -follow -regextype
normal options (always true, specified before other expressions):
-depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
--version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race
tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
-cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
-ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
-links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
-nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
-readable -writable -executable
-wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
-used N -user NAME -xtype [bcdpfls]
-context CONTEXT
actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
-fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
-exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
-execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;
Report (and track progress on fixing) bugs via the findutils bug-reporting
page at http://savannah.gnu.org/ or, if you have no web access, by sending
email to <bug-findutils@gnu.org>.
◆ find 명령어의 기능으로 찾은 파일들의 문자 치환 방법
find ./경로 -name "<파일명>" -exec perl -pi -e "s/<바꿀문자>/<치환문자>/g" {} \;
예) XXX.sp 파일들 모두의 HGD (홍길동)을 LSS (이순신) 으로 바꾸려면
find . -name "*.sp" -exec perl -pi -e "s/HGD/LSS/g" {} \;
물론 방법은 이것만 있는 것은 아니고 cat 명령어와 sed를 통해 실행하거나, vim의 기능을 통해서도 할 수 있습니다. vim의 경우에도 wild card를 사용해서 여러 파일을 연 상태로 실행할 수 있다. 가장 간단하면서 일부는 바로 확인이 가능한 장점이 있지만, script를 작성할 때 중간과정으로 하기에는 불가능한 방법이다. 예전에 vim의 몇몇 기능을 소개한 포스팅을 참고 하시라.
[VIM] 유용한 기능들 - 1탄
unix 계열을 활용한 직업군들에게는 필수적인 툴인 VI (라지만 사실 요즘은 다 VIM이다, 혹은 GVIM). 이게 익숙해지면 새로운 기능이나, 기존과 다르게는 잘 안 쓰게 돼서 모르면 끝까지 모르는 경우
hodman.tistory.com
또 cat과 sed를 통한 방법이 있는데, 앞서 소개한 find의 방법과 비교하자면 cat으로는 한 파일을 열고 여러 가지의 치환을 하는 경우에 좀 더 유용하게 쓰일 수 있고, find의 경우에는 복수의 파일에서 같은 치환 명령을 수행하는데 더 유리하다. 둘 다 script로 작성할 때도 사용할 수 있지만 그 용도에 따라서 선택하도록 하자.
'Programming 언어 > Perl' 카테고리의 다른 글
[Perl] DRC error 항목 select check (2) | 2023.11.09 |
---|