If you start from the parent directory, you can do this with find
and GNU cp
. Assuming the directory you're in currently (the one containing tmp
) is called folder
, and that tmp
is empty, you'd run
cd ..
find . -path ./folder/tmp -prune -o -type f -exec cp --parents -t folder/tmp {} +
This asks find to list everything under .
(which is your old ..
), exclude it if it matches ./folder/tmp
, and otherwise if it's a file pass it to cp
with the --parents
option (which tells cp
to reconstruct the source hierarchy).
If tmp
already has files which also need to be copied, the following variant is slightly less Unix-y (since it uses moreutils
' sponge
) but avoids skipping the contents of tmp
:
cd ..
find . -type f -print0 | sponge | xargs -0 cp --parents -t folder/tmp
You could avoid the use of sponge
by saving the list of files to copy in a file somewhere else (but then things get rather less elegant):
cd ..
find . -type f -print0 > /tmp/filelist
xargs -0 cp --parents -t folder/tmp < /tmp/filelist
rm /tmp/filelist
You can avoid the requirement on GNU cp
by using cpio
instead:
cd ..
find . -type f -print0 > /tmp/filelist
cpio -pmd0 folder/tmp < /tmp/filelist
rm /tmp/filelist
or
cd ..
find . -type f -print0 | sponge | cpio -pmd0 folder/tmp
If tmp
is empty you can also avoid the requirement on moreutils
:
cd ..
find . -path ./folder/tmp -prune -o -type f -print0 | cpio -pmd0 folder/tmp
tmp
was empty but that may well be wrong! – Stephen Kitt May 27 '15 at 20:14cp: illegal option -- -
. I'm actually 3 levels up in the hierarchy. The command looks like this:find . -path ./docker/app/tmp/ -prune -o -type f -exec cp --parents -t docker/app/tmp/ {} +
– user1427661 May 27 '15 at 22:15--
before{}
? As incp --parents -t docker/app/tmp -- {} +
... – Stephen Kitt May 27 '15 at 22:37cp --version
say? Does the lastcpio
variant work? – Stephen Kitt May 28 '15 at 18:03cp --version
gives me the usage screen for cp. I should have mentioned I'm actually doing this on OSX, so it's the BSD cp that ships with the OS. I guess this is one of the cases where the mac utils are annoyingly different than their unix/linux counterparts in subtle ways. The problem is I'm writing this as a util for the team, and most of them develop on macs. It's a tool for building containers, so once I get them in the containers I have control, but before that I can't enforce a specific version of cp or get sponge installed on the system. – user1427661 May 28 '15 at 23:12cp
. I've added a variant without GNUcp
andmoreutils
, it works on Mac OS X (but it doesn't copytmp
so it's appropriate only iftmp
is empty). – Stephen Kitt May 29 '15 at 05:03