Sé que no estoy respondiendo la xargspregunta directamente, pero vale la pena mencionar findla -execopción.
Dado el siguiente sistema de archivos:
[root@localhost bokeh]# tree --charset assci bands
bands
|-- Dream\ Theater
|-- King's\ X
|-- Megadeth
`-- Rush
0 directories, 4 files
El comando find se puede hacer para manejar el espacio en Dream Theater y King's X. Entonces, para encontrar a los bateristas de cada banda usando grep:
[root@localhost]# find bands/ -type f -exec grep Drums {} +
bands/Dream Theater:Drums:Mike Mangini
bands/Rush:Drums: Neil Peart
bands/King's X:Drums:Jerry Gaskill
bands/Megadeth:Drums:Dirk Verbeuren
En la -execopción se {}encuentra el nombre del archivo, incluida la ruta. Tenga en cuenta que no tiene que escapar o ponerlo entre comillas.
La diferencia entre -execlos terminadores ( +y \;) es que +agrupa tantos nombres de archivos como puede en una línea de comando. Mientras \;que ejecutará el comando para cada nombre de archivo.
Entonces, find bands/ -type f -exec grep Drums {} +resultará en:
grep Drums "bands/Dream Theater" "bands/Rush" "bands/King's X" "bands/Megadeth"
y find bands/ -type f -exec grep Drums {} \;dará como resultado:
grep Drums "bands/Dream Theater"
grep Drums "bands/Rush"
grep Drums "bands/King's X"
grep Drums "bands/Megadeth"
En el caso de que grepesto tenga el efecto secundario de imprimir el nombre del archivo o no.
[root@localhost bokeh]# find bands/ -type f -exec grep Drums {} \;
Drums:Mike Mangini
Drums: Neil Peart
Drums:Jerry Gaskill
Drums:Dirk Verbeuren
[root@localhost bokeh]# find bands/ -type f -exec grep Drums {} +
bands/Dream Theater:Drums:Mike Mangini
bands/Rush:Drums: Neil Peart
bands/King's X:Drums:Jerry Gaskill
bands/Megadeth:Drums:Dirk Verbeuren
Por supuesto, greplas opciones -hy -Hcontrolarán si el nombre del archivo se imprime o no, independientemente de cómo grepse llame.
xargs
xargs También puede controlar cómo están los archivos man en la línea de comandos.
xargspor defecto agrupa todos los argumentos en una línea. Para hacer lo mismo que -exec \;sí usa xargs -l. Tenga en cuenta que la -topción le dice xargsque imprima el comando antes de ejecutarlo.
[root@localhost bokeh]# find ./bands -type f | xargs -d '\n' -l -t grep Drums
grep Drums ./bands/Dream Theater
Drums:Mike Mangini
grep Drums ./bands/Rush
Drums: Neil Peart
grep Drums ./bands/King's X
Drums:Jerry Gaskill
grep Drums ./bands/Megadeth
Drums:Dirk Verbeuren
Ver que el -l opción le dice a xargs que ejecute grep para cada nombre de archivo.
Contra el valor predeterminado (es decir, sin -lopción):
[root@localhost bokeh]# find ./bands -type f | xargs -d '\n' -t grep Drums
grep Drums ./bands/Dream Theater ./bands/Rush ./bands/King's X ./bands/Megadeth
./bands/Dream Theater:Drums:Mike Mangini
./bands/Rush:Drums: Neil Peart
./bands/King's X:Drums:Jerry Gaskill
./bands/Megadeth:Drums:Dirk Verbeuren
xargstiene un mejor control sobre cuántos archivos pueden estar en la línea de comando. Dé a la -lopción el número máximo de archivos por comando.
[root@localhost bokeh]# find ./bands -type f | xargs -d '\n' -l2 -t grep Drums
grep Drums ./bands/Dream Theater ./bands/Rush
./bands/Dream Theater:Drums:Mike Mangini
./bands/Rush:Drums: Neil Peart
grep Drums ./bands/King's X ./bands/Megadeth
./bands/King's X:Drums:Jerry Gaskill
./bands/Megadeth:Drums:Dirk Verbeuren
[root@localhost bokeh]#
Ver que grepse ejecutó con dos nombres de archivo debido a -l2.
ls |grep mp3 |sed -n "7p"solo puedes usarecho "Lemon Tree.mp3".