bashxargslocatelnplocate

Use --replace string in xargs but still use multiple arguments per command


I want to pipe plocate output (a list of paths) to another command using xargs. I need to use the -I{} replacement but I want to run one single command with all args, not one command per arg. The problem is that -I option implies -L 1. Example bash code:

plocate -r '/home/me/.*\.jpg' | xargs -I{} ln {} /tmp/images 

this silly code should link all files found by plocate in /tmp/images. This actually works but it runs one command per file! which is slow if I have a lot of images.


Solution

  • Perhaps you don't need xargs at all

    readarray -t a < <(plocate -r '/home/me/.*\.jpg')
    ln -s "${a[@]}" /tmp/images