find
and xargs
are great tools for recursively processing the contents of directories and sub-directories. For example
find . -type f -print0 | xargs -0 command
will run command
on batches of files from the current directory and its sub-directories. The -print0
and -0
arguments avoid the usual problems with filenames that contain spaces, quotes or other metacharacters.
If command
just takes one argument, you can limit the number of files passed to it with -L1
.
find . -type f -print0 | xargs -0 -L1 command
And as suggested by alexgirao, xargs
can also name arguments, using -I
, which gives some flexibility if command
takes options. -I
implies -L1
.
find . -type f -print0 | xargs -0 -Iarg command arg --option