Tôi biết rằng tôi sẽ không trả lời các xargscâu hỏi trực tiếp nhưng nó đáng nói find's -execlựa chọn.
Cho hệ thống tập tin sau:
[root@localhost bokeh]# tree --charset assci bands
bands
|-- Dream\ Theater
|-- King's\ X
|-- Megadeth
`-- Rush
0 directories, 4 files
Lệnh find có thể được thực hiện để xử lý không gian trong Dream Theater và King X. Vì vậy, để tìm các tay trống của mỗi ban nhạc bằng 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
Trong -exectùy chọn {}là viết tắt của tên tệp bao gồm đường dẫn. Lưu ý rằng bạn không phải thoát nó hoặc đặt nó trong dấu ngoặc kép.
Sự khác biệt giữa -execcác đầu cuối ( +và \;) là +các nhóm có nhiều tên tệp có thể nằm trên một dòng lệnh. Trong khi đó \;sẽ thực thi lệnh cho mỗi tên tệp.
Vì vậy, find bands/ -type f -exec grep Drums {} +sẽ dẫn đến:
grep Drums "bands/Dream Theater" "bands/Rush" "bands/King's X" "bands/Megadeth"
và find bands/ -type f -exec grep Drums {} \;sẽ dẫn đến:
grep Drums "bands/Dream Theater"
grep Drums "bands/Rush"
grep Drums "bands/King's X"
grep Drums "bands/Megadeth"
Trong trường hợp grepnày có tác dụng phụ là in tên tệp hay không.
[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
Tất nhiên, grepcác tùy chọn -hvà -Hsẽ kiểm soát xem tên tệp có được in hay không bất kể grepđược gọi như thế nào .
xargs
xargs cũng có thể kiểm soát tập tin man trên dòng lệnh.
xargstheo nhóm mặc định tất cả các đối số trên một dòng. Để làm điều tương tự mà -exec \;sử dụng xargs -l. Lưu ý rằng -ttùy chọn yêu xargscầu in lệnh trước khi thực hiện.
[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
Xem -ltùy chọn này cho xargs thực thi grep cho mỗi tên tệp.
So với mặc định (nghĩa là không có -ltùy chọ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
xargskiểm soát tốt hơn về số lượng tệp có thể nằm trên dòng lệnh. Cung cấp -ltùy chọn số lượng tệp tối đa cho mỗi lệnh.
[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]#
Xem đó grepđã được thực hiện với hai tên tập tin vì -l2.
ls |grep mp3 |sed -n "7p"bạn chỉ có thể sử dụngecho "Lemon Tree.mp3".