개발일기

Linux - find와 grep 명령어 본문

Linux

Linux - find와 grep 명령어

Flashback 2023. 1. 28. 17:33
728x90
반응형

find와 grep은 리눅스 파일 시스템에서 일치하는 컨텐츠를 찾아내는 공통점을 가지고 있다. 하지만 find와 grep은 타겟팅하는 컨텐츠에 큰 차이점을 가지고 있다.

  • find : 이름을 기반으로 일치하는 파일 또는 디렉터리를 찾아낸다.
  • grep : 해당 텍스트를 포함한 파일을 찾아낸다.

1. find 명령어

find [경로] [옵션] [이름]
# 이러한 순서로 find 명령어를 입력하여 실행한다.
find / -name "resources" # resources라는 이름을 가진 파일 또는 디렉터리를 찾아낸다.

find / -type -f -name "fruit.log" # fruit.log라는 이름을 가진 파일을 찾아낸다.

find / -type -d -name "public" # public이라는 이름을 가진 디렉터리를 찾아낸다.
  • -name : 일치하는 이름을 가진 파일 또는 디렉터리를 찾아낸다.
  • -type f : 검색할 타입을 파일로 한정한다.
  • -type d : 검색할 타입을 디렉터리로 한정한다
  • / : 루트 경로에서 검색한다.
  • ./ : 현재 디렉터리 경로를 바탕으로 검색한다. (경로는 사용자가 원하는대로 지정할 수 있다)

1-1. 패턴 추가

find / -name "res*" # res로 시작하는 이름을 가진 파일 또는 디렉터리를 찾아낸다.

*을 활용하여 패턴을 생성할 수 있다. 시작부분에 텍스트를 포함하거나 끝 부분에 텍스트를 포함하는 등의 패턴을 생성하여 find 명령어를 활용할 수 있다.

2. grep 명령어

grep [옵션] [패턴] [파일명]
# 이러한 순서로 grep 명령어를 입력하여 실행한다.
grep "fruit" fruit.txt # fruit.txt에서 fruit이라는 텍스트의 라인을 출력한다.

grep -i "Mango" fruit.txt # fruit.txt에서 대소문자 구별없이 fruit이라는 텍스트의 라인을 출력한다.

grep -r "fruit" # 하위 디렉터리에서 fruit이라는 텍스트를 가지고있는 파일의 해당 라인을 출력한다.
  • -i : 대소문자 구별없이 검색한다.
  • -r : 하위 디렉터리에서 검색을 시작한다.

참고 사이트 :

https://codefather.tech/blog/difference-grep-find/

 

What Is The Difference Between Grep and Find in Linux? - CODEFATHER

I have heard people asking what is the difference between grep and find multiple times in the last few years. Let's find out!

codefather.tech

 

https://www.redhat.com/sysadmin/linux-find-command

 

10 ways to use the Linux find command

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. The content published on this site are community contributions and are for informational purpose only AND ARE NOT, AND ARE NOT INTENDED TO BE, RED

www.redhat.com

 

https://www.geeksforgeeks.org/find-command-in-linux-with-examples/

 

find command in Linux with examples - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

https://www.digitalocean.com/community/tutorials/grep-command-in-linux-unix

 

Grep Command in Linux/UNIX | DigitalOcean

 

www.digitalocean.com

728x90
반응형

'Linux' 카테고리의 다른 글

Linux - 계정 비밀번호 변경  (0) 2023.09.09
Linux - crontab 명령어  (0) 2022.08.13
Linux - Mariadb 백업(export) 및 복원(import)  (0) 2022.08.13
Linux - 사용자 관련 명령어 및 옵션  (0) 2022.02.20
Comments