윈도우

윈도우에서 cmd로 파일 또는 파일 내용 검색

 

# How to do a simple file search in cmd
https://stackoverflow.com/questions/8066679/how-to-do-a-simple-file-search-in-cmd

 


 

dir 명령어를 사용해서 파일 검색하고 find 명령어를 사용해서 파일 내용 검색

 

1. /b 옵션 간략하게, /s 옵션 서브디렉토리포함해서 검색
dir /b/s *.txt

 

2. filelist.txt 에 결과 저장
dir /b/s *.exe >> filelist.txt

 

3. filelist.txt 내용 중에서 “filename” 이 포함된 줄 표시 (/n 옵션은 줄번호 표시)
type filelist.txt | find /n “filename”

 


 

where 명령어 사용해서 파일 검색 (windows 7 에 추가된 명령어)

 

# where 명령어는 기본적으로 PATH 환경 변수의 경로를 검색하기 때문에 /r 옵션을 사용해서 디렉토리를 지정해야한다.

 

# /r 디렉토리 지정 (서브디렉토리 포함)
where /r c:\Windows *.exe *.dll

 

# 검색 결과를 클립보드로 복사
where /r c:\Windows *.exe |clip

 

# 결과가 길다면 — More — 메시지를 표시, space 누루면 한 페이지씩 표시, enter 누루면 한 줄씩 표시
where /r c:\Windows *.exe |more

 

# where 도움말 보기
where/?

 


 

find 명령어

 

# 윈도우 find, findstr 명령어
https://realforce111.tistory.com/10

 

# 파일 내용을 찾는 명령어

 

/V – 지정한 문자열이 없는 줄을 표시합니다.
/C – 지정한 문자열이 있는 줄 수만을 표시합니다.
/N – 지정한 문자열이 있는 각 줄 앞에 줄 번호를 붙입니다.
/I – 대/소문자를 구별하지 않고 찾습니다.

 

# txt 파일 내용에서 hello 가 있는 줄을 표시
find “hello” *.txt

 

# 총 개수만 표시
find /c “hello” *.txt

 

# 앞에 줄번호 붙임
find /n “hello” *.txt

 

# dir 의 결과를 find 에 전달
dir | find “txt”

 

# find, findstr 두 개 동시 사용
netstat -na | findstr “ESTABLISHED” | find “443”

 


 

findstr 명령어

 

# find 보다 더 많은 옵션을 제공, 정규식 가능

 

# /s 옵션은 서브디렉토리 포함, /n 옵션은 줄번호 표시
findstr /s /n “hello” *.txt

 

# 검색어를 두 개 이상 찾으려면 공백으로 분리 (“aaa” 또는 “bbb”)
findstr /s /n “aaa bbb” *.txt

 

# 검색어를 그대로 찾으려면 /c 옵션을 붙인다. (“aaa bbb”)
findstr /s /n /c:”aaa bbb” *.txt

 


 

마이크로소프트 findstr 설명서

https://docs.microsoft.com/ko-kr/windows-server/administration/windows-commands/findstr

 

# 파일 x.y 에서 hello 또는 there 검색
findstr hello there x.y

 

# 파일 x.y 에서 “hello there” 을 검색
findstr /c:hello there x.y

 

# 대문자 W로 시작하는 Windows 검색
findstr Windows proposal.txt

 

# 서브디렉토리를 포함해서 대소문자 구분없이 windows 검색
findstr /s /i Windows *.*

 

# 0개 이상의 공백으로 시작하는 For
findstr /b /n /r /c:^ *FOR *.txt

 

# /f:파일목록파일, /g:검색어목록파일, 결과를 results.txt 로 저장

findstr /g:stringlist.txt /f:filelist.txt > results.txt

 

# 단어 computer 를 검색, /m 옵션은 파일의 이름만 표시
findstr /s /i /m <computer> *.*

 

# comp로 시작하는 단어를 검색
findstr /s /i /m <comp.* *.*

 


 

# cmd ( && , | ) ( type , findstr )
https://jdh5202.tistory.com/149

 

 

&& 와 &

 

&&(and) : ” 앞에 명령어가 잘 실행되면 && 뒤의 명령어도 실행하라”
& : ” 앞에 명령을 실행하고 뒤의 명령을 실행하라 ”

 

echo hi my name is … > test.txt && test.txt

# hi my name is …라는 문자열을 test.txt 파일에 출력한 후 test.txt를 메모장으로 연다.

 


 

| 와 ||

 

|(파이프라인) : ” 앞에 명령어의 처리결과를 뒤의 명령어로 전달한다 ”
|| : ” 앞의 명령 실행이 실패하면 뒤의 명령을 실행하라 ”

 

type test.txt | findstr “abcd”
# test.txt파일에서 “abcd”라는 문자열을 찾는다.

 

certutil /? | findstr /S /N /I “get”
# 특정 명령어 메뉴얼의 내용 중 필요한 부분만 대소문자 구분없이 검색

 

bcdedit > nul || (echo 우클릭 하여 관리자 권한으로 실행하세요. & pause & exit)
# bcdedit은 관리자 권한으로 실행된다. 앞의 명령이 실패할 경우 관리자 권한으로 실행하도록 유도하는 메시지를 띄운다.

 

Related posts

Leave a Comment