#TIL #bash 특정 파일 패턴을 제외하고 모두 삭제

$ find . -type f ! -name '*.txt'  -delete

현재 디렉터리에서 *.txt 패턴에 일치하지 않는 파일을 지운다.

The first part of the expression is recognised by the fact that it begins with ‘-’ followed by some other letters (for example ‘-print’), or is either ‘(’ or ‘!’. Any arguments after it are the rest of the expression.

- 8.1 Invoking find - gnu.org

! 연산자를 사용할 수 있다. ! -name ‘*.txt’ 으로 쓰면 *.txt 패턴이 아닌 이름을 찾는다.

-delete 옵션을 사용해 찾은 파일을 지운다.

참고 - In linux, how to delete all files EXCEPT the pattern *.txt? - unix.stackexchange.com

Feedback plz <3 @ohyecloudy, ohyecloudy@gmail.com

A Random Post