The find
operator {}
, used with the
-exec
(17.10
)
operator, only works when it's separated from
other arguments by white space.
So, for example, the following command will not
do what you thought
it would:
% find . -type d -exec mkdir /usr/project/{} \;
You might have thought this command would
make a duplicate set of - pty) directories, from the
current directory and down, starting at the directory /usr/project
.
For instance, when the find
command finds the directory ./adir
,
you would have it execute mkdir
/usr/project/./adir
(ignore the dot; the result is /usr/project/adir
) (1.21
)
.
That doesn't work because find
doesn't recognize the {}
in the
pathname.
The trick is to pass the directory names to
sed
(34.24
)
,
which substitutes in the leading pathname:
% find . -type d -print | sed 's@^@/usr/project/@' | xargs mkdir
% find . -type d -print | sed 's@^@mkdir @' | (cd /usr/project; sh)
Let's start with the first example.
Given a list of directory names, sed
substitutes the desired path to that directory at the beginning of the line
before passing the completed filenames to
xargs
(9.21
)
and mkdir
.
An
@
is used as a sed
delimiter (34.7
)
because slashes (/) are needed in the actual text of the substitution.
If you don't have xargs
, try the second example.
It uses sed
to insert the mkdir
command, then changes
to the target directory in a
subshell (13.7
)
where the
mkdir
commands will actually be executed.