파일/디렉토리 regex 조건으로 골라 지우기

Taek goo Kim·2022년 1월 6일
0

파일이나 디렉토리를 regex 조건으로 골라 지우기

리눅스나 클라우드 원격 서버에 붙어서 작업을 하다보면, 조건에 맞추어 파일이나 디렉토리를 지우고 싶은 경우가 있습니다.

파일이나 디렉토리 갯수가 너무 많거나 이름이 너무 길면 일일이 하나하나 지우기 귀찮습니다.

이를 위해 regex (또는 다른 조건으로)로 걸러내어 조건에 맞는 파일만 지울 수 있습니다.(물론 이것도 귀찮을 수도 있습니다.)

배경

머신러닝 하이퍼파라미터 튜너등을 사용하는 경우에는 아래와 같이 엄청나게 길고 알아보기 까다로운 디렉토리들이 자동으로 생성되는데, 이 중에서 조건에 맞는 일부만 남기고 제거하고자 합니다.

'train_wrapper_3de7c8c4_6_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-05_19-07-08'
'train_wrapper_51446ba2_2_activation=ReLU,batch_norm_continuous_input=0,dropout=0.64966,embedding_dropout=0.539,layers=1024-512-256_2022-01-06_17-11-57'
'train_wrapper_52ed0206_3_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-05_17-27-30'
'train_wrapper_6e2bf7a6_9_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-06_10-17-35'
'train_wrapper_715bf2ec_1_activation=ReLU,batch_norm_continuous_input=0,dropout=0.51659,embedding_dropout=0.65878,layers=1024-512-2_2022-01-06_16-44-13'
'train_wrapper_9836c47e_5_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-05_18-33-52'
'train_wrapper_baeb5a96_8_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-06_09-43-56'
'train_wrapper_d88e8084_4_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-05_17-59-52'
'train_wrapper_deae0d30_2_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-05_16-55-37'
'train_wrapper_e67d4674_1_activation=ReLU,dropout=0.5,layers=1024-512-256-512-256,use_batch_norm=0_2022-01-05_16-27-12'
'train_wrapper_fc148598_10_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-06_12-01-46'
'train_wrapper_fcbc9764_7_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-06_09-09-59'

명령어

간단하게 아래의 명령어를 수행하면 특정일을 포함한 디렉토리를 삭제할 수 있습니다.

$ ls | grep -E '(.)+2022-01-05_[0-9-]+' | xargs -d"\n" rm -rf
  • ls

    전체 파일과 디렉토리를 목록화합니다.

  • grep -E '(.)+2022-01-05_[0-9-]+'

    파일과 디렉토리 목록에서 2022-01-05를 포함하는 디렉토리와 파일을 필터링합니다.

  • xargs -d "\n" rm -rf

    라인(\n)을 기준으로 끊어서 rm -rf의 파라미터로 넣어주고 반복합니다.

주의사항 1: Regex 조건을 잘못쓰면 잘못된 파일이나 디렉토리가 날아갈 수 있음에 주의하시기 바랍니다.
주의사항 2: 삭제의 조건은 중간의 Regex의 필터링에 따라 다를 수 있습니다.
주의사항 3: Regex에 익숙하지 않다면 추천하지 않습니다.

수행 결과

아래와 같이 2022-01-05를 포함한 디렉토리는 모두 삭제되었습니다.

'train_wrapper_51446ba2_2_activation=ReLU,batch_norm_continuous_input=0,dropout=0.64966,embedding_dropout=0.539,layers=1024-512-256_2022-01-06_17-11-57'
'train_wrapper_6e2bf7a6_9_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-06_10-17-35'
'train_wrapper_715bf2ec_1_activation=ReLU,batch_norm_continuous_input=0,dropout=0.51659,embedding_dropout=0.65878,layers=1024-512-2_2022-01-06_16-44-13'
'train_wrapper_baeb5a96_8_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-06_09-43-56'
'train_wrapper_fc148598_10_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-06_12-01-46'
'train_wrapper_fcbc9764_7_activation=ReLU,dropout=0.75,layers=1024-512-256-128,use_batch_norm=1_2022-01-06_09-09-59'
profile
Unknowns vastly exceeds knowns

0개의 댓글