← 전체로 돌아가기
스킬 shell

Git을 이용해 배쉬에서 사용되지 않는 파일 찾기

Git repo에서 참조 안 되는 파일 쉽게 찾기

gitbashshell-scriptfile-cleanupgrep

Git 레포에서 혹시 참조 안 되고 혼자 남아있는 파일들 있지? git grep이랑 find 조합으로 쉽게 찾아낼 수 있어. 아래 스크립트 돌려보면 됨.

for file in $(find . -type f -not -path "./.git/*"); do
  if ! git grep -q $(basename $file); then
    echo "Unused file: $file"
  fi
done
  • find . -type f -not -path "./.git/*": 현재 디렉토리에서 .git 폴더 제외하고 모든 파일 찾기.
  • git grep -q $(basename $file): 파일 이름(basename)으로 git grep 실행해서 참조되는 곳 있는지 확인. -q는 결과 출력 없이 존재 여부만 체크함.
  • if ! ...: 참조되는 곳 없으면 "Unused file"로 출력.

여기서 배울 것

  1. `find` 명령어로 특정 조건 파일 검색하는 법
  2. `git grep`으로 파일 내용에서 문자열 찾는 법
  3. `basename`으로 파일 경로에서 파일명만 추출하는 법
  4. Bash `for` 루프와 `if` 조건문 활용법
원본 파일 보기 (.claude/skills/tn-find-unused-files-git-bash/SKILL.md)
---
name: Git을 이용해 배쉬에서 사용되지 않는 파일 찾기
description: This skill should be used when the user asks to find potentially unused or unreferenced files within a Git repository using a bash script, by checking if `git grep` finds the file's basename.
version: 1.0.0
source: /home/son/prj/resume/backup_notes_260317/notion/Tech Note/for문으로 배쉬에서 파일 체크하기 9dd476568a0448218e5f0d12e50c038b.md
---

# for문으로 배쉬에서 파일 체크하기

for file in $(find . -type f -not -path "./.git/*"); do
if ! git grep -q $(basename $file); then
echo "Unused file: $file"
fi
done

![Screen Shot 2023-06-28 at 2.25.33 PM.png](for%EB%AC%B8%EC%9C%BC%EB%A1%9C%20%EB%B0%B0%EC%89%AC%EC%97%90%EC%84%9C%20%ED%8C%8C%EC%9D%BC%20%EC%B2%B4%ED%81%AC%ED%95%98%EA%B8%B0/Screen_Shot_2023-06-28_at_2.25.33_PM.png)