Bash: delete based on file date stamp

后端 未结 2 589
难免孤独
难免孤独 2020-12-14 11:51

I have a folder with a bunch of files. I need to delete all the files created before July 1st. How do I do that in a bash script?

相关标签:
2条回答
  • 2020-12-14 12:29

    This should work:

    find /file/path ! -newermt "Jul 01"
    

    To find the files you want to delete, so the command to delete them would be:

    find /file/path ! -newermt "Jul 01" -type f -print0 | xargs -0 rm
    
    0 讨论(0)
  • 2020-12-14 12:32

    I think the following should do what you want:

    touch -t 201007010000 dummyfile
    find /path/to/files -type f ! -newer dummyfile -delete
    

    The first line creates a file which was last modified on the 1st July 2010. The second line finds all files in /path/to/file which has a date not newer than the dummyfile, and then deletes them.

    If you want to double check it is working correctly, then drop the -delete argument and it should just list the files which would be deleted.

    0 讨论(0)
提交回复
热议问题