find-tip
· #find · #hacks · #linux · #shell
I had to figure this out today to automate some log cleanup:
- find all files in the current directory
- that are older than 30 days
- but not in directories
DIR1
,DIR2
orDIR3
- print the matching file names
- and delete them
This is what I came up with:
find . -regextype posix-extended -type f -a -not -regex "^\.\/(DIR1\/|DIR2\/|DIR3\/).*" -a -mtime +30 -exec rm -vf {} \;
I tried doing combinations of -wholepath
but it just didn’t work. Further, I didn’t want to use grep
to filter out the directories because there’s no corresponding flag in grep
to handle the -print0
output from find
.