2016-08-02 1 views
0

45 일보다 오래된 파일을 찾아서 압축하여 보관 폴더로 이동하고 원본 파일을 삭제해야합니다.파일 찾기, 압축 해제 및 다른 위치로 이동, 원본 파일 삭제

find . -name '*.dat' -exec zip '{}.zip' '{}' ';' -exec mv '{}' ~/archive/ \; 

위의 명령은 작동하지만 원본 파일은 여전히 ​​폴더에 있습니다. 위의 명령

-m 
    --move 
      Move the specified files into the zip archive; actually, this 
      deletes the target directories/files after making the specified 
      zip archive. If a directory becomes empty after removal of the 
      files, the directory is also removed. No deletions are done 
      until zip has created the archive without error. This is useful 
      for conserving disk space, but is potentially dangerous so it is 
      recommended to use it in combination with -T to test the archive 
      before removing all input files. 
+0

왜 멋진 스크립트를 만들지 않습니까? –

+0

다른 유스 케이스로 파일을 정리하는 스크립트가 있고 대부분이 하나의 라이너입니다. 그래서 저는 find를 사용하여 모든 것을 한 줄에 넣으려고 노력할 것입니다. – coolnags

+0

스크립트 내에서 함수를 만들 수 있습니다. 그 함수를 호출하는 것도 1 행입니다. –

답변

0

남자 우편, BTW
.. 그렇다면 왜 삭제 이동? 이것은 만족스럽지 않은 것 같습니다.

0

# find . -mtime +45 -name '*.dat' -exec zip '{}.zip' '{}' ';' -exec mv '{}' ../. ';' -exec rm '../{}' \;

괜찮

+0

설명과 혼동 스러울 수 있습니다. 파일을 압축하려고 시도했을 때 원본 파일을 그대로 둡니다. 45 일이 넘은 파일을 찾아서 압축하여 보관 디렉토리로 이동하면됩니다. – coolnags

+0

명령은 "find. -name"* .gz * "-print | barges -I mv {} ~/archive 명령은 정상적으로 작동하지만 다음과 같이 오류가 발생합니다. mv : '/ home//test.dat.gz '및'/ home//test.dat.gz '는 같은 파일입니다. 파일이 올바르게 이동하려고합니다. 이 오류의 원인은 무엇입니까 – coolnags

+0

경로가 어디에서 실행 되었습니까? 'find'가'archive'를 발견 했으므로 –