0

I have 12 directores:

/home/imp/hpt/boxes/110.110.2.0/
/home/imp/hpt/boxes/115.115.16.0
/home/imp/hpt/boxes/1.154.10.0/
/home/imp/hpt/boxes/44.100.0.0/
/home/imp/hpt/boxes/46.1.100.0/

/home/imp/hpt/outbound/
/home/imp/hpt/outbound.002/
/home/imp/hpt/outbound.02c/
/home/imp/hpt/outbound.02e/
/home/imp/hpt/outbound.06e/
/home/imp/hpt/outbound.073/
/home/imp/hpt/outbound.38f/

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?

Thanks.

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

#!/bin/bash

dir1=/home/imp/hpt/boxes/110.110.2.0/
dir2=/home/imp/hpt/boxes/115.115.16.0/
dir3=/home/imp/hpt/boxes/1.54.10.0/
dir4=/home/imp/hpt/boxes/44.100.0.0/
dir5=/home/imp/hpt/boxes/46.1.100.0/
dir6=/home/imp/hpt/outbound/
dir7=/home/imp/hpt/outbound.002/
dir8=/home/imp/hpt/outbound.02c/
dir9=/home/imp/hpt/outbound.02e/
dir10=/home/imp/hpt/outbound.06e/
dir11=/home/imp/hpt/outbound.073/
dir12=/home/imp/hpt/outbound.38f/

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
1
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
0

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.