根据 Linux 上一年以上的日期创建十万个文件的 ZIP

Create ZIP of hundred thousand files based on date newer than one year on Linux

我有一个 / 文件夹,其中包含过去 10 年创建的超过 50 万个文件。我正在重组流程,以便将来有基于年份的子文件夹。

目前,我需要备份去年修改过的所有文件。我试过了

zip -r /backup.zip $(find /folder -type f -mtime -365

但出现错误:参数列表太长。

是否有其他方法可以压缩和存档文件?

您必须从一次传递所有文件切换到一次一个地通过管道将文件传送到 zip 命令。

find /folder -type f -mtime -365 | while read FILE;do zip -r /backup.zip $FILE;done

您还可以在 find 中使用 -exec 参数,如下所示: find /folder -type f -mtime -365 -exec zip -r /backup.zip \; (或任何你的命令)。对于每个文件,执行给定的命令时都会将文件作为最后一个参数传递。

找到文件,然后使用 + 而不是 ;

对尽可能多的文件执行 zip 命令
find /folder -type f -mtime -365 -exec zip -r /backup.zip '{}' +

Zip 具有从 stdin 读取文件列表的选项。以下来自 zip 手册页

-@ file lists. If a file list is specified as -@ [Not on MacOS], zip takes the list of input files from standard input instead of from the command line. For example,

      zip -@ foo

will store the files listed one per line on stdin in foo.zip.

这应该可以满足您的需求

find /folder -type f -mtime -365 | zip -@ /backup.zip

请注意,我删除了 -r 选项,因为它没有做任何事情 - 您正在使用 find 命令明确选择标准文件 (-type f)