Use current filename (“{}”) multiple times in “find -exec”?

前端 未结 3 485
小蘑菇
小蘑菇 2020-12-08 19:08

Many sources say that every instance of {} will be replaced with the filename found through find, but when I try to run the following, I only get one text file and its name

3条回答
  •  醉话见心
    2020-12-08 19:27

    To deal with the caveats that William Pursell mentioned in his answer, use the following:

    find /directory -name "*pattern*" -exec sh -c 'cut -f8 "$1" > "$1.txt"' x {} \;
    

    When you use sh -c, it gets the positional parameters from arguments following the string to execute. The extra x fills in $0, and the substituted filename will become $1.

    The double quotes allow this to work properly with filenames containing spaces and other special characters.

提交回复
热议问题