Динамическое применение команд chmod и chown к выходным данным команды find

Я провел небольшое тестирование и в своей системе выполнил следующее - ни один из них не увеличил скорость на порядок, необходимый для конкурентоспособности, но вы можете сделать это быстрее:

Тест 1: 18,233 с

#!/bin/bash
i=0
while [[ $i -le 4000000 ]]
do
    let i++
done

test2: 20.45s

#!/bin/bash
i=0
while [[ $i -le 4000000 ]]
do 
    i=$(($i+1))
done

test3: 17.64s

#!/bin/bash
i=0
while [[ $i -le 4000000 ]]; do let i++; done

test4: 26.69s

#!/bin/bash
i=0
while [ $i -le 4000000 ]; do let i++; done

test5: 12.79s

#!/bin/bash
export LC_ALL=C

for ((i=0; i != 4000000; i++)) { 
:
}

Важной частью этого последнего является экспорт LC_ALL = C. Я обнаружил, что многие операции bash в конечном итоге выполняются значительно быстрее, если это используется, в частности, любая функция регулярного выражения. Он также показывает недокументированный синтаксис для использования {} и: в качестве запрета.

2
27.02.2017, 14:19
2 ответа

tee клонирует канал, а xargs передает каждой команде аргументы:

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

Используйте флаг -exec команды find для запуска команд с результатами:

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.

{} - это токен подстановки для имен файлов, которые будут передать на найти .

5
27.01.2020, 21:51

Теги

Похожие вопросы