Hacker News new | past | comments | ask | show | jobs | submit login

Please don't use -exec. Use -print0 and xargs -0 instead, as it's much safer, and correctly passes newlines and other problem characters. Even BSD adopted this idiom.

GNU find says this very specifically: "There are unavoidable security problems surrounding the -exec action..."




I'm pretty sure it's a bit faster too. Or for some reason I moved to it.


It would be faster because the exec takes place for every single result of the find.

Simple xargs builds the longest possible command line with as many find results as can fit.

If the -n option were used with xargs, it might slow down to similar performance as -exec, but it would still be safer.

Multiple processes can also be launched by the xargs, increasing performance by using multiple CPUs.

Here is a script using xargs -0 to launch 6 compression processes at a time on files specified to a shell script:

    #!/bin/sh

    N_CPUs=6

    EXT="xz"

    S="$(mktemp -t PARALLEL-XXXXXX)"

    printf '#!/bin/sh \n exec xz -9e "$1"' > "$S"

    chmod 500 "$S"

    if [[ -z "$1" ]]
    then echo "Specify files to pack into ${EXT} files."
    else for x
         do printf '%s\0' "$x"
         done | xargs -0 -P "$N_CPUs" -Ifname "$S" fname
    fi

    rm -f "$S"


> It would be faster because the exec takes place for every single result of the find.

Only if you end the exec with \;

If you end it with + then the results are built up and appended to the command much like xargs.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: