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?


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.

| improve this answer | |
  • 2
    While abstruse one-liners are impressive in their own way and certainly display meritorious mastery of complex shell (or other) incantations, I have opted to increasingly err on the side of simplicity and readability. That said, if the file structure being replicated is fantastically complex, a single filesystem pass would be the superior option. – DopeGhoti Jan 18 '17 at 20:55
  • 1
    Depends on personal background, some people think in branches, others in sequences. If a once-off project then one command is easier to type, but to be reused and maintained, discrete steps are cleaner. – Chindraba Jan 18 '17 at 21:19

With GNU coreutils (e.g.. on non-embedded Linux or Cygwin), it's as simple as

cp -a --attributes-only Dir1 Dir2
| improve this answer | |
cd /path/to/oldroot
find . -type d -exec mkdir -p \"/path/to/newroot/{}\"\;
find . -type f -exec touch \"/path/to/newroot/{}\"\;
| improve this answer | |
  • the above command is throwing error. find: missing argument to -exec'` – learnningprogramming Jan 18 '17 at 21:13
  • @learnningprogramming In addition to the syntax error, the double quotes are neither necessary nor desirable. – xhienne Jan 18 '17 at 23:39

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.