find 명령의 출력에 chmod 및 chown 명령을 동적으로 적용합니다.

find 명령의 출력에 chmod 및 chown 명령을 동적으로 적용합니다.

루트 디렉터리의 여러 디렉터리에 파일이 있습니다. 모든 파일에 chmod 640 및 chown 명령을 적용해야 합니다. 파일 경로를 찾는 명령과 chmod 및 chown을 적용하는 명령이 있습니다. find 명령의 출력에 chmod 및 chown을 적용하는 방법

예:

find . -type f -name 'myawesomeapp.jar'

chmod 640 /path/to/file/myawesomeapp.jar
chown root:webapps /path/to/file/myawesomeapp.jar

chmod 640 /path/to/another/file/myawesomeapp.jar
chown root:webapps /path/to/another/file/myawesomeapp.jar

답변1

결과에 대해 명령을 실행하려면 find의 -exec 플래그를 사용하십시오.

find . -type f -name 'myawesomeapp.jar' -exec chmod 640 {} \+ -exec chown root:webapps {} \+

귀하의 경우 exec의 두 번째 변형을 사용하고 싶습니다.

-exec command ;
    Execute command; true if 0 status is returned.  All following  argu‐
    ments  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 construc‐
    tions 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  sur‐
    rounding 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  invocations  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 allowed within the com‐
    mand.  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.

{}전달될 파일 이름의 대체 토큰입니다 find.

답변2

tee파이프라인을 복제하고 xargs각 명령에 인수를 제공합니다.

find -name 'myawesomeapp.jar' -print0 | tee >(xargs -0 chown root:webapps) | xargs -0 chmod 640

관련 정보