单个命令查找文件,压缩并移动到不同位置,删除原始文件

时间:2016-08-02 16:29:10

标签: linux shell ksh

我需要查找超过45天的文件,将其压缩并移动到存档文件夹并删除原始文件。

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

上面的命令似乎有效,但原始文件仍在文件夹中。

3 个答案:

答案 0 :(得分:0)

在这里采用Ipor的想法是很多示例和难题find

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

答案 1 :(得分:-1)

man zip

   -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.

答案 2 :(得分:-1)

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

以上命令没问题,
顺便说一句..为什么,然后移动删除?这似乎并不满足。