Linux: delete every file older than date with one exclusive file

I can delete if I allow all ordinary files in a folder older than 7 days in:

find /path/to/dir -type f -mtime +7 -exec rm {} \;

with one problem. Here is a file (.gitignore) that I want to save. I tried using regex but apparently negative lookhhead is not supported for findutils regex(?!gitignore)

Any other ideas?

+4
source share
1 answer

Use ! -name .gitignore

find /path/to/dir ! -name .gitignore -type f -mtime +7 -exec rm {} \;

You can group multiple arguments in copied brackets. For example, to delete all files except .gitignorejavascript files (ending in .js):

find /path/to/dir ! \( -name ".gitignore" -o -name "*.js" \) -type f -mtime +7 -exec rm {} \;

-o means or

+5
source

All Articles