Copying folders without their included files

JaredBenson

Registered
Hi there,

This may be a silly question, but is it possible to copy a list of folders which have files in them to a new destination without copying the files inside the folders?

The reason I ask is that I need to copy a large list of folders with long names to a new drive, but they have very large files in them, so it takes a long time to copy everything and delete the files afterwards.

Thanks.
 
OK. It is a trivial task to copy empty folders from one Mac volume to another. However, you cannot copy file-containing folders without also copying the files inside. If you want to do this, then you would have to remove the files from each folder before copying it. However, this is unnecessary. If you want empty folders that have the same names as folders in another volume, then create new folders and give each the same name as a folder in the source volume.
 
If directory owners and rights does not matter, following might help.

Let us assume that you have two directories, "source" and "target". You need to duplicate "source" directory's hierarchy on "target". Use terminal.app (or iTerm.app)

1. Let us assume that source and target are sibling directories
$ mkdir target
$ cd target
$ (cd ../source; find . -type d -print0) | xargs -0 mkdir -p

The line has some interesting features. () around cd and find create a new process, that has its own current directory (if the "source" was not sibling, use its full path. The command find is used in finding different kinds of files; in this case directories (-type d). Option -print0 prints the files to standard output. The output is not shown, since it is piped (the | character) to another program (xargs). Since the pipe is after ending ), it is run in the current process's directory (target). Command xargs takes the outputted directory names (the -0 in xargs and -print0 in find makes the file names end with a special character NUL, that cannot be part of a normal file name) and runs mkdir command with option -p and some (either all at once or several times). mkdir -p creates the directory with the full path.

So, if you had directories a, a/b, a/b/c and d in source, the find will echo

./a\0
./a/b\0
./a/b/c\0
./d\0

(where I used \0 instead of the NUL character), and mkdir -p will create ./a, ./a/b, ./a/b/c and ./d. The first time will create a, then a/b, then a/b/c and then d.

So, the directory hierarchy is copied, but you will be the directory owner, the directory timestamps will be the current time and if there were any special ACLs, they are dropped.
 
Back
Top