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

Try:

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

But be aware that some versions of find require {} to be a distinct argument, and will not expand {} to a filename otherwise. You can work around that with:

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

(this alternate command will put the output file in the subdirectory which contains the matched file. If desired, you could avoid that by redirecting to ${0#*/}

The issue is that find is not doing the redirection, the shell is. Your command is exactly equivalent to:

# Sample of INCORRECT code
find /directory -name "*pattern*" -exec cut -f8 {} \; > {}.txt

Note the following from the standard:


If more than one argument containing only the two characters “{}” is present, the behavior is unspecified.

If a utility_name or argument string contains the two characters “{}” , but not just the two characters “{}” , it is implementation-defined whether find replaces those two characters or uses the string without change.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)