Linux – create empty files with same directory structure as reference directory

cplinux

I have a Dir1 with multiple subdir and files inside it. I intend to copy the Dir1 to Dir2 so that all the files in it will be just empty files but with same file name as Dir1.

Then I intend to push the Dir2 to github to explain example data-structure and filenames to users.

Is there a command to copy files in a way just to destination files are empty but with same filename?

Best Answer

Or more complicatedly but with a single filesystem pass (for even more portability ~ should be written as $HOME)

find . \( -type d -exec mkdir -p "~/elsewhere/{}" \; \
  -o -type f -exec touch "~/elsewhere/{}" \; \)

The complexity here is that of Boolean logic (which may be of some benefit to learn) and precedence (also good to know) and how find implements these concepts with an implicit AND between the -type and subsequent action, and OR making an appearance as -o.

Related Question