find: Interspersing File Names
1
1 3.3.2.6 Interspersing File Names
1 ................................
1
1 'xargs' can insert the name of the file it is processing between
1 arguments you give for the command. Unless you also give options to
1 limit the command size (⇒Limiting Command Size), this mode of
1 operation is equivalent to 'find -exec' (⇒Single File).
1
1 '--replace[=REPLACE-STR]'
1 '-I REPLACE-STR'
1 '-i REPLACE-STR'
1 Replace occurrences of REPLACE-STR in the initial arguments with
1 names read from the input. Also, unquoted blanks do not terminate
1 arguments; instead, the input is split at newlines only. For the
1 '-i' option, if REPLACE-STR is omitted for '--replace' or '-i', it
1 defaults to '{}' (like for 'find -exec'). Implies '-x' and '-l 1'.
1 '-i' is deprecated in favour of '-I'. As an example, to sort each
1 file in the 'bills' directory, leaving the output in that file name
1 with '.sorted' appended, you could do:
1
1 find bills -type f | xargs -I XX sort -o XX.sorted XX
1
1 The equivalent command using 'find -execdir' is:
1
1 find bills -type f -execdir sort -o '{}.sorted' '{}' ';'
1
1 When you use the '-I' option, each line read from the input is
1 buffered internally. This means that there is an upper limit on the
1 length of input line that 'xargs' will accept when used with the '-I'
1 option. To work around this limitation, you can use the '-s' option to
1 increase the amount of buffer space that xargs uses, and you can also
1 use an extra invocation of xargs to ensure that very long lines do not
1 occur. For example:
1
1 somecommand | xargs -s 50000 echo | xargs -I '{}' -s 100000 rm '{}'
1
1 Here, the first invocation of 'xargs' has no input line length limit
1 because it doesn't use the '-I' option. The second invocation of
1 'xargs' does have such a limit, but we have ensured that it never
1 encounters a line which is longer than it can handle.
1
1 This is not an ideal solution. Instead, the '-I' option should not
1 impose a line length limit (apart from any limit imposed by the
1 operating system) and so one might consider this limitation to be a bug.
1 A better solution would be to allow 'xargs -I' to automatically move to
1 a larger value for the '-s' option when this is needed.
1
1 This sort of problem doesn't occur with the output of 'find' because
1 it emits just one filename per line.
1