Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive warning for find #4

Closed
arth1 opened this issue Jul 24, 2013 · 3 comments
Closed

False positive warning for find #4

arth1 opened this issue Jul 24, 2013 · 3 comments
Assignees

Comments

@arth1
Copy link

arth1 commented Jul 24, 2013

This warning is invalid:

find trunk -type f -name '*.gcda' -print0 |\
^-- Don't use find | xargs cmd. find -exec cmd {} + handles whitespace.

find ... -print0 | xargs -0 ... handles whitespace.
And piping to xargs can be orders of magnitude(!) faster than using -exec {}, which is single-threaded, blocking, and not file system cache friendly:

$ rm -f /tmp/foo; time find /usr/lib64 -type f 2>/dev/null -print0 | xargs -0 md5sum >>/tmp/foo 2>/dev/null

real 0m3.304s
user 0m2.971s
sys 0m0.373s
$ rm -f /tmp/foo; time find /usr/lib64 -type f 2>/dev/null -exec md5sum >>/tmp/foo {} ;

real 0m14.842s
user 0m4.928s
sys 0m5.174s

If anything, the warning should be the other way - never use exec {} if there's an opportunity to use -print0 | xargs -0

@koalaman
Copy link
Owner

You're quite right: it should not be complaining when using -print0. I'll fix this shortly.

You did ignore its advice and used "-exec {} ;" instead of "-exec {} +" though, which is why your alternative is slower :P

@ghost ghost assigned koalaman Jul 24, 2013
@arth1
Copy link
Author

arth1 commented Jul 24, 2013

Thanks for fixing!

Actually, even with exec {} +, find is notably slower than xargs. The main reason is that it blocks the find process while doing a block of exec.

$ time find . -type f -exec ls -d >/dev/null {} +

real 0m3.790s
user 0m2.368s
sys 0m1.346s
$ time find . -type f -exec ls -d >/dev/null {} +

real 0m3.779s
user 0m2.404s
sys 0m1.295s
$ time find . -type f -exec ls -d >/dev/null {} +

real 0m3.806s
user 0m2.412s
sys 0m1.315s
$ time find . -type f -print0 | xargs -0 ls -d >/dev/null

real 0m3.122s
user 0m2.644s
sys 0m1.372s
$ time find . -type f -print0 | xargs -0 ls -d >/dev/null

real 0m3.157s
user 0m2.628s
sys 0m1.343s
$ time find . -type f -print0 | xargs -0 ls -d >/dev/null

real 0m3.185s
user 0m2.633s
sys 0m1.387s
$ find . -type f | wc -l
175730

... and never mind that you can also pass -P NNN to xargs :P

$ time find . -type f -print0 | xargs -0 -P 8 ls -d >/dev/null

real 0m0.810s
user 0m3.503s
sys 0m1.585s

@koalaman
Copy link
Owner

Shellcheck no longer warns about piping find to xargs -0

gerow pushed a commit to gerow/shellcheck that referenced this issue Jul 29, 2017
@dengyufei123 dengyufei123 mentioned this issue May 19, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants