4 ms·
My main usecase for find is not only finding files, but to do actions on them through -exec. Unless I’m missing something, that’s not supported with this tool.
by josteink 9y ago
My main usecase for find is not only finding files, but to do actions on them through -exec.
Unless I’m missing something, that’s not supported with this tool.
- sharkdp 9y agoThank you for the feedback! '-exec' is not supported at the moment. It's definitely something I would consider adding. We do have '-0'/'--print0' to separate results by NULL, so 'fd' can be used in combination with 'xargs'.
- lloeki 9y agoOr parallel, which is slightly trickier to implement than xargs/-exec.
- jitl 9y agoYou can easily compose this with `xargs` using the`-0 / --print0` option: fd -0 '\.log$' | xargs -0 tail
- tyingq 9y agoxargs does have limits on input length. On some platforms they aren't terribly high. Sound like -exec is potentially coming though. Edit: Swore I had gotten ARG_MAX complaints out of xargs in the past. Replies are correct though. I'm wrong here...xargs adjusts on the fly. Perhaps long ago, or some obscure use of xargs?
- Hello71 9y ago> xargs does have limits on input length what?
- btashton 9y agoYeah they can be as low as 4096 bytes. `xargs --show-limits`
- sitharus 9y agoSome versions of xargs don't respect the shell's max argument length, so on older platforms (MacOS X 10.4 sticks out in my memory) it's easy to cause xargs to try invoking with a longer command line than permitted. This is fixed with the `-n` argument to xargs, which lets you specify the number of arguments per invocation.
- derriz 9y agoWhy is this a problem? Except in terms of performance, it's completely invisible to the user as xargs will run the command multiple times to ensure all the arguments are processed. In the most extreme/unrealistic case, if xargs could only support invoking the command with a single arguement, then the performance would regress to that of using -exec. Otherwise it's much faster.
- derriz 9y agoI'm curious why you (or anyone) uses -exec? It's often painfully slower than piping through xargs and I find the syntax (the semicolon and braces and the required quoting/escaping) uncomfortable. I haven't used -exec in 20 years.
- oconnore 9y agofind ... -ok ... {} \; is easier to type than find ... -print0 | xargs -0 -p -I{} ... {}
- derriz 9y agoInteresting but I was asking about -exec?
- ekns 9y agoIt's not even slower (and it's safe beyond the argv limit) if you use "+" instead of the semicolon. find -type f -exec sed -i s/foo/bar/ {} + This only invokes the command twice for 100k files (assuming we can pass in 64k arguments to sed)
- derriz 9y agoThanks! I never knew about the + terminator.
- pm215 9y agoYou need it if you want to modify the directory structure that find is traversing, so as to affect subsequent traversal, eg http://www.chiark.greenend.org.uk/~pmaydell/find/primes http://www.chiark.greenend.org.uk/~pmaydell/find/primes (cheatsheet here: http://www.chiark.greenend.org.uk/~pmaydell/find/primes-commented http://www.chiark.greenend.org.uk/~pmaydell/find/primes-comm...) Admittedly I wrote that nearly 20 years ago so I guess it doesn't invalidate your point :-)
- kec 9y agoWhat about commands which only take a single file as input? Is xargs -n1 noticeably faster than -exec? (And if it is, is that actually worth it for clarity's sake?)