TL;DR:
Q: How to keep a counter in find -exec
loop ?
My use-case:
I need to move a lot of directories which are scattered around the place, so I do
find . -type d -name "prefix_*" \
-exec sh -c '
new_path="/new/path/$(basedir "$1")";
[ -d "$new_path" ] || mv "$1" "$new_path";
' find_sh {} \;
(The real command is more complex, as I read some metadata for the constitution of /new/path
. Anyways, I do not want to argue about the command itself, it's not part of the question, just the use-case).
It works just fine, but it takes quite a while and I want to keep track of the progress.
So I added a counter writing to a file:
i=$(cat ~/find_increment || echo 0);
echo $((i+1)) | tee ~/find_increment;
That also works just fine, but it feels like a really bad idea having some 100.000 disk read and write operations.
I thought about writing to ramdisk
instead of disk, but I don't have that option in the environment I need to perform that task.
Is there a better way to keep a counter between -exec
runs ?
find_sh
in the first command? So basically you want to know the number of new paths created?find_sh
is just the way how to use sh -c safely./dev/shm
... ?!blktrace
).