You have a deep directory tree of ﬁles, and you need to flatten it into a single directory, so that there are no ﬁles in subdirectories, and all the ﬁles are in the same place.
cpcommand), or not linked (using the
You obviously need to set
$ROOT_DIRECTORY_OF_TREE to the root directory with the ﬁles you want to flatten (or just replace it in the cd command).
The same holds for the
$FLAT_DIRECTORY, which is the destination.
find constructs a list of all ﬁles, and feeds it to
xargs, which builds a suitable command line and executes it for each ﬁle (one at a time, in this example).
The dot (
.) indicates that
find should start with the current directory. On GNU
find (used on Linux), this is the default and can be left out. On every other version of the utility (e.g. on MacOS), you have to include it.
-print0 (in ﬁnd) and
-0 (in xargs) ensure all special characters are handled properly (except the two characters that no Unix can deal with in a ﬁlename, namely
-I%%% option does all the magic — it replaces subsequent instances of the string
%%% with ﬁles from the standard input, one by one.
If your destination directory is on the same ﬁlesystem as the source directory tree, you're better oﬀ using
ln (but not
ln -s) instead of
cp. It's much faster and uses up just one inode and not a single byte more.
An added bonus of
ln is it won't overwrite existing ﬁles by default, so you'll get an error message for each duplicate ﬁlename (i.e. ﬁlenames breaking the precondition above).