![-exec 명령을 아시나요? [복사]](https://linux55.com/image/159261/-exec%20%EB%AA%85%EB%A0%B9%EC%9D%84%20%EC%95%84%EC%8B%9C%EB%82%98%EC%9A%94%3F%20%5B%EB%B3%B5%EC%82%AC%5D.png)
이게 효과가 있어
find ./ -iname '*.c' -o -name '*.h' -o -name '*.l' -exec grep -irn test1 {} \;
ls와 함께 사용 하면 -exec
이것이 잘못된 옵션이라고 알려줍니다.
#ls -exec touch {} \;
ls: invalid option -- 'e'
Try 'ls --help' for more information.
이 -exec
옵션은 다른 명령이 아닌 find에서만 사용된다는 것이 내 이해가 맞습니까?
답변1
명령을 이해하는 가장 좋은 방법은 해당 설명서를 읽는 info ls
것 info find
입니다 man ls/man find
. 또는 각 POSIX 사양에 대해 알아보고 모든 다른 구현에 공통적으로 적용되는 하위 집합을 이해하세요.
구문 은 다음 find
과 같습니다
find [options] <file1> [<file2>...] [predicates]
(일부 find
구현에서는 건너뛰기를 허용합니다 <file1>
)
그리고 ls
:
ls [options] [<file1> [<file2>...]]
( [...]
옵션 부분을 나타냅니다).
표준 옵션은 단일 문자이며 -
. 어떤 사람들은 그 주장을 받아들이고, 어떤 사람들은 받아들이지 않습니다. 옵션은 매개변수로 결합될 수 있습니다. 예를 들어 로 ls -l -d
쓸 수 있습니다 ls -ld
.
-exec
따라서 이는 옵션이 될 수 없다는 것을 알 수 있습니다 . find
이는 옵션이 다음과 같이 해석되기 때문입니다 -e -x -e -c
(또는 옵션이 인수를 허용 -e -x ec
하는 경우 -x
).
일부 도구는 지원을 구현합니다.긴옵션이 있지만 이러한 충돌을 피하기 --
위해 이러한 옵션 앞에는 접두사가 붙습니다. -
예를 들어, GNU find
와 GNU는 모두 ls
지원합니다 .--help
옵션.
의 경우 find
POSIX -exec
호출술부. 그것은 아니다옵션. 그것은표현하다이는 파일 목록 뒤에 위치하며 어떤 파일이 선택되고 해당 파일에 대해 어떤 작업이 수행되는지 결정하는 데 사용됩니다.
find
일부 옵션도 지원됩니다(앞으로-L
/ 와 같은 파일 목록)은 -H
전역 동작에 영향을 미칩니다.
ls
개념 없음술부...도 아니다표현하다파일을 선택하는데 사용됩니다. 그 행동은 오직 다음에 의해서만 통제됩니다.옵션. ls
임의의 명령을 실행할 수 있는 옵션은 없습니다 .
그런데 코드에 몇 가지 문제가 있습니다.
-a
(생략된 경우 암시됨)이 보다 우선하므로 파일에 대해서만 실행-o
됩니다 . 바라보다-exec
*.l
여러 개의 '-name' 및 '-exec'가 있는 'find'는 마지막으로 나타나는 '-name'만 실행합니다.-r
를 사용하곤 했습니다 .grep
이것은 작업을 완료하고 디렉토리에서 파일을 찾기grep
위해 일부 구현에서 지원되는 비표준 옵션 입니다.find
발견된 파일 중 해당find
디렉터리 유형이 없으면 해롭지 않을 수 있지만 필요하지 않을 수도 있습니다.- 구문을 사용하면 파일당 하나씩 실행할
{} \;
수 있습니다 .grep
이는 매우 비효율적입니다. 이는 또한grep
파일 이름이 표시되지 않음을 의미합니다(-r
위에서 시작하지 않는 한). - 이는
-iname
비표준 확장입니다(info find
GNU 확장인 경우 시스템에서 찾을 수 있지만 POSIX 사양에서는 찾을 수 없음). -type
조건자를 사용하지 않으므로find
일반 파일뿐만 아니라 모든 유형의 파일이 보고됩니다. 여기에는 디렉터리, 장치, FIFO가 포함될 수 있습니다. 를 사용하면-type f
검색이 다음으로 제한됩니다.정기적인문서. GNU를 사용하면 심볼릭 링크 확인 후 일반 파일이 되는 파일을 선택할find
수도 있습니다 .-xtype f
이러한 문제를 해결하려면 다음을 수행할 수 있습니다.
find . -name '*.[cChl]' -type f -exec grep -in test1 /dev/null {} +
답변2
매뉴얼 페이지에서 find
설명하는 것처럼 매뉴얼 페이지에서는 ls
전혀 설명하지 않지만 -exec
에 특정한 매개변수입니다 find
. 그렇기 때문에 에서는 작동 하지만 에서는 -exec
작동 하지 않습니다 .find
ls
매뉴얼 페이지에는 다음과 find
같이 나와 있습니다.
-exec command ;
Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `{}' is
replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these
constructions might need to be escaped (with a `\') or quoted to protect them from expansion by the shell. See the EXAMPLES section for examples of the use of the -exec option. The specified
command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the
-execdir option instead.
-exec command {} +
This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invoca‐
tions of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines. Only one instance of `{}' is al‐
lowed within the command. The command is executed in the starting directory. If find encounters an error, this can sometimes cause an immediate exit, so some pending commands may not be run
at all. This variant of -exec always returns true.
-execdir command ;
-execdir command {} +
Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method
for invoking commands, as it avoids race conditions during resolution of the paths to the matched files. As with the -exec action, the `+' form of -execdir will build a command line to
process more than one matched file, but any given invocation of command will only list files that exist in the same subdirectory. If you use this option, you must ensure that your $PATH envi‐
ronment variable does not reference `.'; otherwise, an attacker can run any commands they like by leaving an appropriately-named file in a directory in which you will run -execdir. The same
applies to having entries in $PATH which are empty or which are not absolute directory names. If find encounters an error, this can sometimes cause an immediate exit, so some pending commands
may not be run at all. The result of the action depends on whether the + or the ; variant is being used; -execdir command {} + always returns true, while -execdir command {} ; returns true
only if command returns 0.
이 ls
명령은 그러한 매개변수를 허용하지 않습니다.