OS/CentOS&Rocky

파일 내용 찾기, 바꾸기

Lawmin 2025. 7. 14. 11:38

1-1. 현재 경로 하위 전체 text file 의 내용 중 ABC 로 시작하는 부분 찾기

※ \< 를 앞에 줘서 단어 기준임, ex) ABCdef(O), defABC(X), "ABCdef"(O)

find . -type f -exec sh -c '
  for file; do
    if file --mime-type "$file" | grep -q "text/"; then
    grep "\<ABC" "$file"
    fi
  done
' sh {} +

 

1-2. 현재 경로 하위 전체 text file 의 내용 중 ABC 로 시작하는 부분 찾기 (빠르나 부정확할 수 있음)

\< 를 앞에 줘서 단어 기준임, ex) ABCdef(O), defABC(X), "ABCdef"(O)

find . -type f -exec sh -c '
  for file; do
    grep -I "\<ABC" "$file"
  done
' sh {} +

 

2. 현재 경로 하위 전체 text file 의 내용 중 ABC를 DEF 로 바꾸기

 \b 를 앞에 줘서 단어 기준임, ex) ABCdef(O), defABC(X), "ABCdef"(O)

find . -type f -exec sh -c '
  for file; do
    if file --mime-type "$file" | grep -q "text/"; then
      sed -i "s/\bABC/DEF/g" "$file"
    fi
  done
' sh {} +