0

I have a find command that selects some directories and files to be copied to a different location (with --parents). What I'd like is for the directories that are output by the find command to be copied without contents (but they should have the same ownership and mode).

I could limit find to files only with -type f or ! -type d, but then directories would be created as needed with the default ownership and mode. If I use the -r option to cp, then directories are copied with the right ownership and mode, but also with all their files. Basically, I'd like for directories in the listing to result in copying only the directory metadata without its contents.

How to achieve this?

  • rsync might be a better tool for this than find + cp – steeldriver Nov 27 '16 at 1:16
2

The simplest tool to use I think is pax, as it has a -d option which says not to recurse when given a directory.

So to copy just directories, doing it in steps, so you can validate that the directories are copied do not copy the files as well you can do

find somewhere -type d | pax -wd  > archive.tar
tar vtf archive.tar
( cd somewhere_else && tar xpf - ) < archive.tar

Once you are happy, you can remove the intermediate printing of the contents of the archive (the tar vxf archive.tar) and replace the intermediate archive.tar file with a pipe.

  • 1
    Well, the better known tar tool can do the same thing with the --no-recursion option; e.g., find ... -print0 | tar -c --no-recursion --null -T - | tar -C /destination/directory -x – DepressedDaniel Nov 27 '16 at 1:51
  • You may know the --no-recursion option to tar, but this is by no means standard. – icarus Nov 27 '16 at 9:06
1

If you have GNU find, maybe something like

find path/to/dir -type d -printf '%P\0' | 
  rsync -0av --files-from=- path/to/dir/ path/to/newdir/

will do what you want (test by replacing -0av with -0avn).

Depending what your search criteria are, you might be able to use rsync alone with suitable filters e.g. to copy an entire directory structure

rsync -avn -f'+ */' -f'- *' path/to/dir/ path/to/newdir/

See for example Linux: rsync Copy Directories Structures Tree Only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.