C.W.K.
Stream
Lesson 10 of 10 · published

xargs 와 tee

~12 min · xargs, tee, parallel

Level 0창 구경꾼
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

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 — 전체 출력 저장 + 에러만 화면 모니터링.

Code

Find + delete 안전하게·bash
# Print first to verify
find . -name '*.tmp' -print
# Then delete via xargs (-0 for spaces in names)
find . -name '*.tmp' -print0 | xargs -0 rm -v
빌드 출력 저장 + 모니터링·bash
./build.sh 2>&1 | tee build.log | grep -i 'error\|warning'
# Append to a running log
make test 2>&1 | tee -a test.log

External links

Exercise

*.tmp 파일 만들기: touch /tmp/{a,b,c}.tmp. 이어서 find /tmp -name '*.tmp' -print0 | xargs -0 -I {} -P 2 rm -v {}. 병렬 삭제 확인. seq 5 | tee count.log 로 화면 + 파일 동시 출력 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.