I have 12 directores:



I want to remove files that are under ~5k in each of these directories. The files are *.mo? *.tu? *.we? *.th? *.fr? *.sa? *su? where ? is a number from 0 to 9

I've read about inotifywait, and was wondering if it could be used to achieve this. Is it possible?


Edit: Ok, here is a inotifywait script i'm trying to write.. I didn't get very far as you can see.



inotifywait --daemon --outfile /home/imp/hpt/remove.log -m "$dir1" "$dir2" "$dir3" "$dir4" "$dir5" "$dir6" "$dir7" "$dir8" "$dir10" "$dir11" "$dir12" -e     delete |
    while read path action file; do
        for name in "$dir1'" "$dir2" "$dir3" "$dir4" "$dir5" "$dir6" "$dir7" "$dir8" "$dir9" "$dir10" "$dir11" "$dir12"

Can someone help me out?

  • Yes. That is correct. – ignatius Jan 9 '18 at 21:36
find . \( -name '*.mo[0-9]' -o -name '*.tu[0-9]' \) -size -5120c -delete

You can use size -5k, too, but find does some stupid rounding so this is less precise:

but bear in mind that the size is rounded up to the next unit (so a 1-byte file is not matched by -size -1M)

  • 1
    find ./ -regex ".*mo\.*tu\.*sa[0-9]" -size -5k – Roman Grazhdan Jan 9 '18 at 22:04
  • 1
    @RomanGrazhdan I guess you haven't tried that. – Hauke Laging Jan 9 '18 at 22:08
  • Thanks for your answer. But, what exactly does the -o flag do? I need to figure out how to add the rest of the "day" filenames. – ignatius Jan 10 '18 at 0:18
  • Ok. checked the find manpage. I guess -o is OR – ignatius Jan 10 '18 at 0:33
  • 1
    Ok. Here's the script now: find . \( -name '*.mo[0-9]' -o -name '*.tu[0-9]' -o -name '*.we[0-9]' -o -name '*.th[0-9]' -o -name '*.fr[0-9]' -o -name '*.sa[0-9]' -o -name '*.su[0-9]' \) -size -5120c -delete Does that look correct? – ignatius Jan 10 '18 at 0:40

inotify and inotifywait watch for filesystem actions in real-time. You could use them to catch the files as they are created, but they can't help you find files that already exist there.

I'll assume you want to find existing files. With GNU find, the expression to find them could be condensed a bit using a regex pattern:

$ find -type f -size -5120c -regextype awk -regex ".*\.(mo|tu|we|th|fr|sa|su)[0-9]" -delete

(The size 5120 is in bytes, adjust to what you need.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.