다수의 파일 삭제(exec, loop..)

다수의 파일 삭제(exec, loop..)

삭제할 파일이 다음 형식으로 많이 있습니다.

esymac_logEvents.log.5_2017-Feb-06_02-39-17.Z_2017-Feb-08_02-39-14.Z_2017-Feb-09_02-39-14.Z_2017-Feb-11_02-39-11.Z_2017-Feb-13_02-39-08.Z_2017-Feb-16_02-39-05.Z_2017-Feb-18_02-39-3.Z_2017-Feb-20_02-39-02.Z_2017-Mar-02_12-06-57

esymac_logEvents.log.6_2017-Feb-07_02-39-15.Z_2017-Feb-08_02-39-14.Z_2017-Feb-09_02-39-14.Z_2017-Feb-16_02-39-05.Z_2017-Feb-18_02-39-03.Z_2017-Feb-19_02-39-03.Z_2017-Feb-20_02-39-2.Z_2017-Feb-21_02-38-55.Z

esymac_logEvents.log.6_2017-Feb-07_02-39-15.Z_2017-Feb-09_02-39-14.Z_2017-Feb-12_02-39-10.Z_2017-Feb-15_02-39-06.Z_2017-Feb-16_02-39-05.Z_2017-Feb-17_02-39-04.Z_2017-Feb-19_02-39-3.Z_2017-Feb-20_02-39-02.Z_2017-Feb-21_02-38-55.Z

esymac_logEvents.log.6_2017-Feb-10_02-39-12.Z_2017-Feb-15_02-39-06.Z_2017-Feb-18_02-39-03.Z_2017-Feb-19_02-39-03.Z_2017-Feb-20_02-39-02.Z_2017-Feb-21_02-38-55.Z

esymac_logEvents.log.5_2017-Feb-06_02-39-17.Z_2017-Feb-07_02-39-15.Z_2017-Feb-10_02-39-12.Z_2017-Feb-19_02-39-03.Z_2017-Feb-22_02-39-05.Z_2017-Feb-23_02-39-09.Z

esymac_logEvents.log.5_2017-Feb-06_02-39-17.Z_2017-Feb-08_02-39-14.Z_2017-Feb-11_02-39-11.Z_2017-Feb-12_02-39-10.Z_2017-Feb-14_02-39-07.Z_2017-Feb-15_02-39-06.Z_2017-Feb-17_02-39-4.Z_2017-Feb-22_02-39-05.Z_2017-Feb-23_02-39-09.Z

esymac_logEvents.log.6_2017-Feb-09_02-39-14.Z_2017-Feb-13_02-39-08.Z_2017-Feb-17_02-39-04.Z_2017-Feb-19_02-39-03.Z_2017-Feb-21_02-38-55.Z

esymac_logEvents.log.6_2017-Feb-07_02-39-15.Z_2017-Feb-08_02-39-14.Z_2017-Feb-11_02-39-11.Z_2017-Feb-12_02-39-10.Z_2017-Feb-14_02-39-07.Z_2017-Feb-15_02-39-06.Z_2017-Feb-17_02-39-04.Z_2017-Feb-22_02-39-05.Z_2017-Feb-23_02-39-09.Z

제거하고 싶지만 rm() 명령에서 "인수 목록이 너무 큼" 오류가 발생합니다. 유사한 게시물을 확인했지만 스스로 명령을 작성할 수 없는 경우 다음 명령을 얻을 수 있는 방법이 있습니까?

  • 먼저 모든 esymac_logEvents.log.* 파일을 사용하여 매개변수 목록을 생성하고,
  • 루프나 exec 명령을 사용하여 파일을 하나씩 삭제합니다(
    한 번에 하나씩 삭제하지 않을 수도 있고, rm() 함수가 수신할 수 있는 최대 인수 수를 제거하는 것이 바람직함).

건배.

답변1

find /search/dir -name esymac*whatever*pattern* -exec rm \{\} \;

답변2

위와 다른 형식의 모든 파일을 유지하려면 다음을 수행하십시오.

  1. 이것을 실행하십시오. 그러면 FilesToDelete.txt삭제할 파일/디렉토리가 채워집니다 . 나열된 파일이 실제로 삭제하려는 파일인지 확인하려면 이를 확인하세요.

    find /path/to/dir/esymac_logEvents.log* | xargs ls -l > FilesToDelete.txt

  2. 1단계를 확인한 후 다음을 수행합니다.

    find /path/to/dir/esymac_logEvents.log* -type f | xargs rm -f

  3. 삭제해야 할 디렉터리도 있는 경우 2를 건너뛰고 다음을 수행합니다.

    find /dir/that/contains/esymac_logEvents.log* | xargs rm -rf

관련 정보