19

Is there a way to copy directory structure with files but without their contents. I mean that I want exactly the same directories with files names but leaving them blank/empty. I have few example applications in some programming language and I want to rewrite them with my own style but keeping the way that they was organised in files.

I would be grateful for any help.

38

From man cp

--attributes-only don't copy the file data, just the attributes

So , if you want to copy all folders and files that are in somedirectory

do cp -R --attributes-only somedirectory destinationdirectory

3
  • This is exactly what I need. Thank you very much. – dagi12 May 15 '15 at 16:43
  • 4
    +1 because I didn't think someone had already implemented this - really surprised. – mgarciaisaia May 15 '15 at 18:00
  • @mgarciaisaia I was just surprised as you are, when I checked the man page this morning =) – Sergiy Kolodyazhnyy May 15 '15 at 18:02
10

Nice that Ubuntu cp has this feature, but if you should find yourself on a machine that doesn't (BSD-style cp does not, for example, so OS X does not either), it's very easy to do with find. Here it is as a two-liner (for readability):

% cd original_dir
% find . -type d -exec mkdir -p ../copy_dir/{} \;
% find . -type f -exec touch ../copy_dir/{} \;

If copy_dir already exists, you can skip the -p argument to mkdir since find will traverse the directory tree top-down. For large jobs, you can shave off another millisecond or so by terminating the commands with + instead of \; if your find supports it (it probably does).

Edit: The above commands neglected to handle symbolic links, which can be copied with a third run of find (do not terminate this one with +):

% find . -type l -exec cp -R {} ../copy_dir/{} \;

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.