xargs — stdin 을 argument 로
일부 command 는 stdin 을 안 읽어 — rm, mv, git add. argument 로 파일명을 받아. xargs 가 그 다리: stdin 을 읽어서 공백으로 쪼개고 다음 command 의 argument 로 넣어 실행.
알아야 할 flag
-n 1— invocation 당 argument 하나씩.-I {}— placeholder.{}가 argument 로 치환. argument 가 끝에 있지 않을 때 필수.-0— NUL 구분 입력.find -print0와 짝지어 공백 포함 파일명 안전.-P 4— 최대 4 개 병렬. 저렴한 병렬화.-r— stdin 비어 있으면 command 안 돌림 (GNU. macOS BSD 는 이미 그렇게 동작).
tee — 스트림 분기
tee 가 stdin 을 stdout 과 하나 이상의 파일에 동시에 쓴다. 출력을 저장하면서 동시에 보고 싶을 때.
cmd | tee out.log— 파일 저장 + 화면.cmd | tee -a out.log— append.cmd | sudo tee /etc/protected.conf— 전형적 'non-root 파이프라인이 root 권한 파일에 쓰기'.
합쳐서
find . -name '*.log' | xargs gzip — 모든 로그 압축. find . -name '*.log' -print0 | xargs -0 -I {} -P 4 gzip {} — 병렬 + 공백 안전. ./build.sh | tee build.log | grep -i error — 전체 출력 저장 + 에러만 화면 모니터링.