I am trying to compose a Linux shell command but stuck here.
Here is the background:
The system will generate .xml and .xml.error correspondingly under /tmp/A/processed,/tmp/A/not_processed,/tmp/B/processed,/tmp/B/not_processed directories. The overall directories are uncertain. For example /tmp/C /tmp/D...
I need to find all *.xml.err files with content "oracle" under /tmp/A/not_processed and /tmp/B/not_processed. Then need to move the *.xml files to its parent directory which is /tmp/A or /tmp/B and remove *.xml.err files.
Here is the code which should be fine:
find /home/tmp -name \*.err -type f -path "*not_processed*" -execdir grep -l -i "oracle" {} \; -execdir sh -c '
for name do
mv "${name%.*}" ".."
rm -rf "$name"
done' sh {} +
I am trying to optimize the performance by dropping the -for loop by using multiple -execdir parameters of find.
find /home/tmp -name \*.err -type f -path "*not_processed*" -execdir grep -l -i "oracle" {} \; -execdir mv {} .. \; -execdir rm -rf {} \;
However, I was stuck that -execdir mv {} .. \;
I couldn't drop the second extension name .err
. {} should be the array with file paths such as ./a.xml.err ./b.xml.err
. I tried to simply add .err
behind the {}, that is -execdir mv {}.err .. \;
I can get ./a.xml.err.err ./b.xml.err.err
. Now I know how to add .err
extension but stuck in dropping .err
extension for the {}.
Thank you in advance and your efforts will be appreciated.