·터미널 명령어

xargs

텍스트 처리

쓰는 꼴

[cmd] | xargs [cmd2]

들어온 줄을 다른 명령의 인수로 바꿔 넘깁니다. 이름에 빈칸이 있으면 깨지므로 find -print0과 xargs -0을 짝지어야 합니다.

자주 쓰는 옵션

옵션
-nHow many arguments to hand over per run
-IPut the argument where a placeholder such as {} sits
-0Read NUL-separated input, to pair with find -print0
-PRun that many copies at once
-rDo not run the command at all on empty input
-tPrint each command before running it

예시

find . -name '*.log' -print0 | xargs -0 rm

Deletes them even when names hold spaces.

xargs -n1 -P4 curl -O < urls.txt

Four downloads at a time.

ls *.txt | xargs -I{} mv {} old/{}

Uses {} where the file name goes.

파이프로 이어 쓰는 것이 본래 용도입니다. 한 명령이 다 하는 것보다 셋을 잇는 편이 짧습니다.

읽는 방법

  • 대괄호 [ ]는 넣어도 되고 안 넣어도 되는 자리입니다.
  • 점 셋 …은 여러 개를 이어 쓸 수 있다는 뜻입니다.
  • 옵션은 대소문자를 가립니다 — -r과 -R이 다른 명령도 있습니다.

자주 묻는 것

Q. xargs은 무엇을 하나요?

들어온 줄을 다른 명령의 인수로 바꿔 넘깁니다. 이름에 빈칸이 있으면 깨지므로 find -print0과 xargs -0을 짝지어야 합니다.

Q. 어떻게 치나요?

[cmd] | xargs [cmd2] — 대괄호는 생략할 수 있는 자리입니다.

Q. 옵션은 몇 개나 자주 쓰나요?

여기 정리한 것은 6개입니다. 전체 목록은 man xargs에 있습니다. 이 명령은 텍스트 처리 갈래입니다.

같이 보는 명령

man 페이지: man xargs