Estoy usando find para buscar en todos los subdirectorios y el directorio actual para encontrar todos los archivos .py, y luego ejecuto wc -l para encontrar el número de líneas. Sin embargo, cuando uso find con wc, el campo Total se deja, a diferencia de cuando ejecuto wc solo. ¿Alguien sabe por qué ocurre esto?
find . -name \*.py -exec wc -l {} \;
187 ./check.py
43 ./file.py
33 ./mitch.py
...
1014 ./serve.py
41 ./test_scripts/line_graph.py
39 ./welcome.py
Pero no total, pero cuando ejecuto wc -l *.pyen un directorio obtengo un total:
wc -l *.py
187 check.py
43 file.py
94 log_com.py
154 log_results.py
33 mitch.py
1014 serve.py
39 welcome.py
1564 total

awk: ver la respuesta de Grawity.No hay
totalcampo porque solo hay un archivo.Con la acción, ejecuta el comando una vez para cada archivo , con solo un argumento agregado cada vez. Por lo tanto, no hay nada de lo que cada invocación pueda calcular el total.
-exec command {} ;findwcTendrías que usarlo si tienes GNU ; de lo contrario, deberá canalizar la lista de archivos .
-exec command {} +findxargs command(In this answer I'm not going to bother with the usual
-print0suggestion, because if you don't have GNU find, you won't have-print0or even GNU xargs either. However, if your version of xargs does support-d '\n', use that, because the default way xargs parses input isn't very safe.)Note that this might still result in multiple
wcinvocations, because most operating systems (except maybe Hurd) have a limit on the maximum command-line length and/or maximum number of arguments to a single command, which a large number of files might exceed. @slhck's suggestion of using**/*.pywould also hit the same limit (early Unixes having had very low limits is whyxargswas created in the first place).Another alternative is to use
awkfor the final calculation:fuente
find -deleteas an alternative torm -rf *since then.