statistical find -exec
I want to run a command over some percentage of the files that find finds. I'll use puts here instead of the real code.
find . -type f -exec ruby -e "puts '{}' if (rand > 0.2)" \;
I have a lot of files. Is there a way to do this without calling rand or even using Ruby?
I thought about using mod X on the f开发者_如何学JAVAilesize, since the files are more or less random length but I can't figure out a way to tell find to use that in an expression.
This prints 50% of the files:
find . -exec bash -c "echo -n \$RANDOM | tail -c 1 | grep -q [0-4]" \; -print
To execute something, this should work:
find . -exec bash -c "echo -n \$RANDOM | tail -c 1 | grep -q [0-4]" \; -exec my_command \;
I think I found a better way:
find . -type f | ruby -n -e 'puts $_.chop if (rand > 0.5)'
If you have a lot of files, execing a bash shell for each one can chew up resources. The above line starts one long running ruby process that simply processes the input from find. It's much faster.
精彩评论