Search code examples
bashexecxargs

Iterate over find command's output with exec or xargs & pass this as paramater to script


I have to find files & execute a python command on them. There are several thousand files that find command finds. So, instead of running a for loop to iterate over the output of find command, I'm trying to do an xargs over find output.

I am unable to work out how to pass the output of find command as parameter to the script.

find "$DIRLOC" -iname "*.html" |
xargs python3 src/shell/updates/python/updateshere.py <filename from find command's output to go here> "$HASH" {} \;

Please could someone help me with this?


Solution

  • You can do it like this:

    find "$DIRLOC" -iname "*.html" -print0 |
    xargs -0 -I {} python3 src/shell/updates/python/updateshere.py {} "$HASH"
    

    Used -print0 option in find and -0 in xargs to handle filename/directory names with space or other special characters.

    You can handle all in find also using -exec option:

    find "$DIRLOC" -iname "*.html" -exec \
    python3 src/shell/updates/python/updateshere.py {} "$HASH" \;