TIL

[TIL] Window에서 폴더 안에 있는 특정 확장자 파일 지우기

happykoa 2020. 11. 8. 00:28

github.com/shinkeonkim/Today_PS

와 같이 PS 레포를 관리하고 있는데, C/C++의 경우에 컴파일/실행을 하기 위해 exe 파일들이 생긴다.

 

예전에는 그냥 무시했는데, 점점 이게 많아지면서 생각보다 거슬렸다.

그래서, 한번 이걸 자동적으로 지우는 shell 명령어를 짜두자는 생각을 했다.

 

근데, 내가 만약 Ubuntu를 주 OS로 썼다면 간단했겠지만, 

현재 내 주 OS는 Window이기 때문에, 스택오버플로우를 뒤졌고 아래 링크를 찾았다.

stackoverflow.com/questions/23767489/how-can-one-delete-files-in-a-folder-matching-a-regular-expression-using-powersh

 

How can one delete files in a folder matching a regular expression using PowerShell?

I know a common characteristic of the file names of a number of unwanted files on my Windows computer. How can I remove all of these files from a given folder or folder hierarchy with a single regu...

stackoverflow.com

 

지우려는 목록은 보는 명령어는 아래 명령어다.

Get-ChildItem .\ -Recurse | Where{$_.Name -Match "[0-9]+.exe"}

 

지우는 명령어는 아래 명령어이다.

Get-ChildItem .\ -Recurse | Where{$_.Name -Match "[0-9]+.exe"} | Remove-Item

 

내가 사용하는 파일들은 숫자로만 파일 이름이 되어 있고, 확장자가 exe이기 때문에 위와 같이 썼지만,

다른 파일 이름 형식 + 확장자라면 "[0-9]+.exe" 부분을 수정하여 사용하면 된다.(정규표현식으로 나타내면 된다.)

 

그리고 나서, 항상 저 명령어를 치기는 귀찮으니 ps1 확장자로 파일을 저장해놓고

Window Powershell을 이용해 .\파일명을 입력하면 실행할 수 있다.

 

Rmx