간단기법

[PowerShell] 디렉토리에서 postfix 없애기

씨알메리 2022. 9. 17. 18:03

개요

Github에서 소스코드를 zip파일로 다운받으니, 레포 이름 뒤에 "-master"가 붙더라. "-master"라는 postfix를 없애려면 어떻게 해야하나?

방법

 손으로 하나하나 지워도 되지만.... 스크립트를 써보자. 윈도우니까 파워셀을 이용해보자.

파워셀 Rename-Item

친절하게 예제도 있다.

Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '.txt','.log' }

위 문장과 정규식을 이용하여 다음과 같이 하면 변경할 수 있다.

Get-ChildItem *-master | Rename-Item -NewName {$_.Name -replace '-master', ''}