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 is “.txt”
find /directory -name "*pattern*" -exec cut -f8 {} > {}.txt \;
The goal was to create a text file with only the eighth column from each file found, and each text file will be named after its parent file. Something about that second set of {} is not replacing with the filename of each found file.
Try:
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:(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
findis not doing the redirection, the shell is. Your command is exactly equivalent to: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.