Shell Script — Get all files modified after

前端 未结 9 2489
礼貌的吻别
礼貌的吻别 2020-12-12 09:57

I\'d rather not do this in PHP so I\'m hoping a someone decent at shell scripting can help.

I need a script that runs through directory recursively and finds all fil

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 10:32

    This will work for some number of files. You want to include "-print0" and "xargs -0" in case any of the paths have spaces in them. This example looks for files modified in the last 7 days. To find those modified before the last 7 days, use "+7".

    find . -mtime -7 -print0 | xargs -0 tar -cjf /foo/archive.tar.bz2
    

    As this page warns, xargs can cause the tar command to be executed multiple times if there are a lot of arguments, and the "-c" flag could cause problems. In that case, you would want this:

    find . -mtime -7 -print0 | xargs -0 tar -rf /foo/archive.tar
    

    You can't update a zipped tar archive with tar, so you would have to bzip2 or gzip it in a second step.

提交回复
热议问题