xargs 명령어 (AIX 5.3·6.1)
https://archive.horanglab.com/posts/aix-cmd-xargs
핵심 정리
- 표준 입력에서 인수를 읽어 명령줄을 만들고 명령을 실행합니다. 명령줄 길이 한도(
ARG_MAX)를 넘지 않게 나눠 여러 번 실행합니다. -n N은 한 번에 인수 N개,-L N은 입력 N줄씩 넘깁니다.-I {}는 입력 한 줄을 명령 안의{}자리에 넣습니다.-t는 실행할 명령을 표준 오류에 보여 주고,-p는 실행할지 묻습니다.-x는 명령줄이-s크기를 넘으면 멈춥니다.- 입력에서 밑줄(
_)만 있는 줄을 만나면 입력의 끝으로 봅니다.-E 문자열로 바꿉니다. -e,-i,-l은 옛 플래그라 각각-E,-I,-L을 씁니다. 소문자 플래그와 값 사이에는 빈칸을 두지 않습니다.- AIX 5.3과 6.1 문서의 내용이 같습니다.
| 항목 | 내용 |
|---|---|
| 버전 | AIX 5.3, AIX 6.1 (두 버전 내용 같음) |
| 원문 | IBM Commands Reference — 5.3 Volume 6, 6.1 Volume 6 |
| 파일 | /usr/bin/xargs |
| 관련 명령어 | find, ar, diff, lint |
목적
인수 목록을 만들어 명령을 실행합니다.
영어 원문
Constructs parameter lists and runs commands.
구문
xargs [ -p ] [ -t ] [ -x ] [ -e [ EOFString ] ] [ -E EOFString ] [ -i [ ReplaceString ] ]
[ -I ReplaceString | -L Number | -n Number ] [ -l [ Number ] ] [ -s Size ] [ Command [ Argument ... ] ]
참고: 소문자 플래그와 그 값 사이에는 빈칸을 두지 않습니다.
설명
만들어지는 명령줄의 길이는 Command와 각 Argument를 문자열로 본 바이트 크기의 합이며, 문자열마다 끝의 NULL 바이트를 포함합니다. xargs는 명령줄 길이를 제한합니다. 만든 명령줄을 실행할 때 Argument 목록과 환경 목록을 합친 크기는 ARG_MAX 바이트를 넘을 수 없습니다. 이 제약 안에서 -n이나 -s를 지정하지 않으면 기본 명령줄 길이는 적어도 LINE_MAX 값입니다.
영어 원문
The generated command line length is the sum of the size, in bytes, of the Command and each Argument treated as strings, including a null byte terminator for each of these strings. The xargs command limits the command line length. When the constructed command line runs, the combined Argument and environment lists can not exceed ARG_MAX bytes. Within this constraint, if you do not specify the -n or the -s flags, the default command line length is at least the value specified by LINE_MAX.
플래그
| 플래그 | 설명 |
|---|---|
-e[EOFString] |
옛 플래그이므로 -E를 씁니다. EOFString을 논리적 EOF 문자열로 씁니다. -e나 -E를 지정하지 않으면 밑줄(_)을 논리적 EOF 문자열로 봅니다. EOFString 없이 -e만 쓰면 논리적 EOF 기능을 끄고 밑줄을 글자 그대로 다룹니다. EOF나 지정한 문자열에 이를 때까지 표준 입력을 읽습니다. |
-E EOFString |
기본값인 밑줄(_) 대신 쓸 논리적 EOF 문자열을 지정합니다. EOF나 지정한 문자열에 이를 때까지 표준 입력을 읽습니다. |
-i[ReplaceString] |
옛 플래그이므로 -I(대문자 i)를 씁니다. ReplaceString을 지정하지 않으면 {}를 씁니다. |
-I ReplaceString |
(대문자 i) 표준 입력의 줄마다 Command의 인수로 넣되, Argument 안의 ReplaceString 자리마다 넣습니다. ReplaceString은 인수 5개까지만 쓸 수 있습니다. 표준 입력 줄 앞의 공백은 무시합니다. Argument에는 ReplaceString을 하나 이상 넣을 수 있지만 255바이트를 넘을 수 없습니다. -I는 -x도 켭니다. -I는 -L 1과 같아서 한 번에 표준 입력 한 줄만 바꿔 넣으며, ReplaceString이 여러 번 나오면 같은 줄을 모두에 넣습니다. |
-l[Number] |
(소문자 L) 옛 플래그이므로 -L을 씁니다. Number를 지정하지 않으면 1을 씁니다. -l은 -x도 켭니다. |
-L Number |
표준 입력에서 읽은 비어 있지 않은 인수 줄을 Number줄씩 넘겨 Command를 실행합니다. 남은 줄이 Number보다 적으면 마지막 실행은 그보다 적은 줄로 합니다. 줄의 마지막 문자가 빈칸이나 탭이 아니면 첫 줄바꿈에서 줄이 끝납니다. 끝의 빈칸은 다음 비어 있지 않은 줄로 이어진다는 뜻입니다. 읽은 줄은 명령줄 끝에 붙습니다. |
-n Number |
표준 입력의 인수를 최대 Number개까지 써서 Command를 실행합니다. 읽은 인수는 명령줄 끝에 붙습니다. 쌓인 명령줄 길이가 -s Size를 넘거나, 마지막 반복에서 남은 인수가 Number보다 적으면(0은 아님) 그보다 적게 씁니다. |
-p |
Command를 실행할지 묻습니다. 만든 명령줄 뒤에 ?...(물음표, 줄임표)를 붙여 보여 주며, 로캘에 맞는 긍정 답을 입력하면 실행하고 다른 답이면 그 실행을 건너뜁니다. 실행마다 묻습니다. -p는 -t도 켭니다. |
-s Size |
만들 명령줄의 최대 전체 크기를 정합니다. 양의 정수여야 합니다. 인수 수가 -n을 넘거나, 줄 수가 -L·-l을 넘거나, Size만큼 쌓이기 전에 EOF에 이르면 그보다 적은 인수를 씁니다. |
-t |
추적 모드를 켜서, 만든 명령줄을 실행하기 전에 표준 오류에 보여 줍니다. |
-x |
명령줄이 -s Size보다 크면 xargs를 멈춥니다. -I나 -l을 지정하면 켜집니다. -i, -I, -l, -L, -n을 지정하지 않으면 명령줄 전체 길이가 -s Size 한도 안이어야 합니다. |
참고: -I(대문자 i), -i, -L(대문자 L), -l, -n은 서로 배타적이며, 마지막에 지정한 것이 적용됩니다.
영어 원문
-e[EOFString] — Obsolete flag. Use the-Eflag. Uses the EOFString parameter as the logical EOF string. If you do not specify the-eor the-Eflags, underscore (_) is assumed for the logical EOF string. If you do not specify the EOFString parameter, the logical EOF string capability is disabled, and underscores are taken literally. The xargs command reads from standard input until either EOF or the specified string is reached.-EEOFString — Specifies a logical EOF string to replace the default underscore (_). The xargs command reads standard input until either EOF or the specified string is reached.-i[ReplaceString] — Obsolete flag. Use the-I(uppercase i) flag. If you do not specify the ReplaceString parameter, the string is used. Note: The-I(uppercase i), i,-L(uppercase l) , l, and-nflags are mutually exclusive. The last flag specified takes effect.-IReplaceString — (Uppercase i). Inserts each line of standard input as an argument for the Command parameter, inserting it in Argument for each occurrence of ReplaceString. ReplaceStrings can not be used in more than 5 arguments. Blank characters at the beginning of each standard input line are ignored. Each Argument can contain one or more ReplaceStrings, but may not be larger than 255 bytes. The-Iflag also turns on the-xflag. The -I (uppercase i) flag means -L1. Therefore, only one standard input line can be substituted as an argument at a time. If the replaced string appears more than once in the command parameter, the same standard input line is substituted for each occurrence of the replaced string. Note: The-I(uppercase i), i,-L(uppercase l) , l, and-nflags are mutually exclusive. The last flag specified takes effect.-l[Number] — (Lowercase L). Obsolete flag. Use the-Lflag. If you do not specify the Number parameter, a value of 1 is used. The-lflag also turns on the-xflag. Note: The-I(uppercase i), i,-L(uppercase l),-l, and-nflags are mutually exclusive. The last flag specified takes effect.-LNumber — Runs the Command parameter with the specified number of nonempty parameter lines read from standard input. The last invocation of the Command parameter can have fewer parameter lines if fewer than the specified Number remain. A line ends with the first new-line character unless the last character of the line is a space or a tab. A trailing space indicates a continuation through the next nonempty line. Reads the Number lines from the standard input and places them at the end of the command line. Note: The-I(uppercase i), i,-L(uppercase l),-l, and-nflags are mutually exclusive. The last flag specified takes effect.-nNumber — Runs the Command parameter using as many standard input arguments as possible, up to the maximum specified by the Number parameter. Reads the maximum of Number arguments from the standard input and places them at the end of the command line. The xargs command uses fewer arguments if: • The accumulated command line length exceeds the bytes specified by the-sSize flag. • The last iteration has fewer value than the value specified by the Number argument, but not zero, arguments remaining. Note: The-I(uppercase i), i,-L(uppercase l),-l, and-nflags are mutually exclusive. The last flag specified takes effect.-p— Asks whether to run the Command parameter. It displays the constructed command line, followed by a?...(question mark, ellipsis) prompt. Enter an affirmative response specific to the locale to run the Command parameter. Any other response causes the xargs command to skip that particular invocation of the parameter. You are asked about each invocation. The-pflag also turns on the-tflag.-sSize — Sets the maximum total size of the constructed Command line. The Size parameter must be a positive integer. Fewer arguments are used if: 1. The total number of arguments exceeds those specified by the-nflag. 2. The total number of lines exceeds those specified by the-Lor-l(Lowercase L) flags. 3. EOF is reached before the number of bytes specified by the Size parameter are accumulated.-t— Enables the trace mode and echoes the constructed Command line to standard error before running.-x— Stops running the xargs command if any Command line is greater than the number of bytes specified by the-sSize flag. This-xflag is turned on if you specify either the-I(Uppercase i) or-l(Lowercase L) flag. If you do not specify the-i,-I(Uppercase i),-l(Lowercase L),-L, or-nflag, the total length of the Command line must be within the limit specified by the -s Size flag.
종료 상태
| 값 | 뜻 |
|---|---|
| 0 | Command를 실행할 때마다 종료 상태 0을 돌려줬습니다. |
| 1~125 | 조건에 맞는 명령줄을 만들 수 없었거나, Command 실행 중 하나 이상이 0이 아닌 상태를 돌려줬거나, 다른 오류가 났습니다. |
| 126 | Command를 찾았지만 실행할 수 없었습니다. |
| 127 | Command를 찾을 수 없었습니다. |
조건에 맞는 명령줄을 만들 수 없거나, 명령을 실행할 수 없거나, 명령 실행이 신호로 끝났거나, 명령이 종료 상태 255로 끝나면, xargs는 진단 메시지를 쓰고 남은 입력을 처리하지 않고 끝납니다.
영어 원문
This command returns the following exit values:
- 0 — All invocations of the Command parameter returned exit status 0.
- 1-125 — A command line meeting the specified requirements could not be assembled, one or more of the invocations of the Command parameter returned a non-zero exit status, or some other error occurred.
- 126 — Command was found but could not be invoked.
- 127 — Command could not be found.
If a command line meeting the specified requirements cannot be assembled, the command cannot be invoked, an invocation of the command is terminated by a signal, or an invocation of the command exits
with exit status 255. The xargs command will write a diagnostic message and exit without processing any remaining input.
예제
- 파일에 나열한 이름의 파일들에 명령을 씁니다.
xargs lint -a <cfiles
cfiles 파일에 main.c readit.c gettoken.c putobj.c가 들어 있으면 다음 명령을 만들어 실행합니다.
lint -a main.c readit.c gettoken.c putobj.c
cfiles의 파일 이름이 셸 명령줄 하나(LINE_MAX까지)에 다 들어가지 않으면, 들어가는 만큼으로 lint를 실행하고 남은 이름으로 다시 lint를 만들어 실행합니다. 예를 들면 다음과 같습니다.
lint -a main.c readit.c gettoken.c . . .
lint -a getisx.c getprp.c getpid.c . . .
lint -a fltadd.c fltmult.c fltdiv.c . . .
이것은 모든 파일 이름으로 lint를 한 번 실행하는 것과 다릅니다. lint는 파일 사이의 상호 참조를 검사하는데, 이 경우 main.c와 fltadd.c처럼 다른 명령줄에 있는 파일 사이는 검사하지 못합니다. 모든 이름이 한 줄에 들어갈 때만 실행하려면 -x를 씁니다. 다 들어가지 않으면 오류 메시지를 보여 줍니다.
xargs -x lint -a <cfiles
- 파일 이름을 정해진 수만큼 담은 명령을 만듭니다. 파일 이름을 두 개씩 담은(
-n 2)diff명령을 만들어 실행합니다.-t가 실행 전에 명령을 보여 주고,<<EOF와EOF는 끝 줄 앞까지 입력한 글을xargs의 표준 입력으로 쓰는 here 문서입니다.
xargs -t -n 2 diff <<EOF
starting chap1 concepts chap2 writing chap3
EOF
만들어지는 명령:
diff starting chap1
diff concepts chap2
diff writing chap3
- 명령줄 가운데에 파일 이름을 넣습니다. 현재 디렉터리의 모든 파일 이름 끝에
.old를 붙여 이름을 바꿉니다.-I는ls목록의 줄마다{}자리에 넣으라는 뜻입니다.
ls | xargs -t -I {} mv {} {}.old
현재 디렉터리에 chap1, chap2, chap3이 있으면 다음 명령을 만듭니다.
mv chap1 chap1.old
mv chap2 chap2.old
mv chap3 chap3.old
- 하나씩 골라 명령을 실행합니다.
lib.a라이브러리에 더할 파일을 고릅니다.-p가 만든ar명령을 보여 주고 실행할지 묻습니다.y를 입력하면 실행하고, 다른 키를 누르면 실행하지 않습니다.
ls | xargs -p -n 1 ar r lib.a
ar r lib.a chap1 ?...
ar r lib.a chap2 ?...
ar r lib.a chap3 ?...
- 정해진 수의 인수를 명령줄 가운데에 넣습니다. 현재 디렉터리에
chap1부터chap10까지 있으면 다음과 같이 출력합니다.
ls | xargs -n6 | xargs -I{} echo {} - some files in the directory
chap1 chap2 chap3 chap4 chap5 chap6 - some files in the directory
chap7 chap8 chap9 chap10 - some files in the directory
영어 원문
- To use a command on files whose names are listed in a file, type:
xargs lint -a <cfiles
If the cfiles file contains the following text: main.c readit.c gettoken.c putobj.c
the xargs command constructs and runs the following command: lint -a main.c readit.c gettoken.c putobj.c
If the cfiles file contains more file names than fit on a single shell command line (up to LINE_MAX), the xargs command runs the lint command with the file names that fit. It then constructs and runs another lint command using the remaining file names. Depending on the names listed in the cfiles file, the commands might look like the following: lint -a main.c readit.c gettoken.c . . . lint -a getisx.c getprp.c getpid.c . . . lint -a fltadd.c fltmult.c fltdiv.c . . .
This command sequence is not quite the same as running the lint command once with all the file names. The lint command checks cross-references between files. However, in this example, it cannot check between the main.c and the fltadd.c files, or between any two files listed on separate command lines. For this reason you may want to run the command only if all the file names fit on one line. To specify this to the xargs command use the -x flag by typing:
xargs -x lint -a <cfiles
If all the file names in the cfiles file do not fit on one command line, the xargs command displays an error message.
- To construct commands that contain a certain number of file names, type:
xargs -t -n 2 diff <<EOF starting chap1 concepts chap2 writing chap3 EOF
This command sequence constructs and runs diff commands that contain two file names each (-n 2): diff starting chap1 diff concepts chap2 diff writing chap3
The -t flag causes the xargs command to display each command before running it, so you can see what is happening. The <<EOF and EOF pattern-matching characters define a here document, which uses the text entered before the end line as standard input for the xargs command.
- To insert file names into the middle of command lines, type:
ls | xargs -t -I {} mv {} {}.old
This command sequence renames all files in the current directory by adding .old to the end of each name. The -I flag tells the xargs command to insert each line of the ls directory listing where (braces) appear. If the current directory contains the files chap1, chap2, and chap3, this constructs the following commands:
mv chap1 chap1.old
mv chap2 chap2.old
mv chap3 chap3.old
- To run a command on files that you select individually, type:
ls | xargs -p -n 1 ar r lib.a
This command sequence allows you to select files to add to the lib.a library. The -p flag tells the xargs command to display each ar command it constructs and to ask if you want to run it. Type y to run the command. Press the any other key if you do not want to run the command. Something similar to the following displays:
ar r lib.a chap1 ?...
ar r lib.a chap2 ?...
ar r lib.a chap3 ?...
- To construct a command that contains a specific number of arguments and to insert those arguments into the middle of a command line, type:
ls | xargs -n6 | xargs -I{} echo {} - some files in the directory
If the current directory contains files chap1 through chap10, the output constructed will be the following:
chap1 chap2 chap3 chap4 chap5 chap6 - some files in the directory
chap7 chap8 chap9 chap10 - some files in the directory
파일
| 파일 | 설명 |
|---|---|
/usr/bin/xargs |
xargs 명령이 들어 있습니다. |
관련 명령어: ar, diff, lint
영어 원문
- /usr/bin/xargs — Contains the xargs command.
Related information:
- ar command
- diff command
- lint command
리눅스(GNU)와 다른 점
| 항목 | AIX 5.3·6.1 | GNU xargs |
|---|---|---|
| 논리적 EOF 기본값 | 밑줄(_)만 있는 줄에서 입력을 끝냄 |
기본은 끔 |
-I의 한도 |
ReplaceString은 인수 5개까지, 인수는 255바이트까지 | 사실상 제한 없음 |
-0 |
없음 | NUL로 나눈 입력(find -print0과 함께) |
AIX 5.3·6.1 문서에는 -0, -P(병렬 실행), -r(입력이 없으면 실행 안 함), -d(구분자) 같은 GNU 플래그가 없습니다. 공백이 든 파일 이름은 -I {}로 한 줄씩 넘기는 것이 안전합니다.