Find, exec and bash redirections
- EN
- ES
It is common to launch commands of this type: search for a series of files and, once found, write a certain string in each of them. A practical example would be to search for the scheduler files for disk devices and write in them the name of the desired elevator.
You may want to do somethink like this:
find /sys/block/sd*/queue -maxdepth 1 -name scheduler \
-exec echo "noop" > {} \;
Something doesn’t add up, right? That redirection doesn’t look good. In fact, we’re most likely going to end up with a file named ‘{}’ in the current directory, containing as many ’noop’ lines as devices found.
There are several alternatives, but the simplest one is the following:
find /sys/block/sd*/queue -maxdepth 1 -name scheduler \
-exec sh -c 'echo "noop" > {}' \;
The redirection will be interpreted correctly, and we’ll get the desired result.