0

Within my Downloads I have like 500 folders within it. Within most of those 500 folders I have other folders within them. I want to copy only the folders within this Downloads directory. This includes the folders within the folders.

In the end the objective is to have a have a new directory with like 500 folders and nearly 500 sub-folders and without a single file within them.

Can it be done? I am ready to test this out.

0
1

what you want is called copying directories structure , try rsync

rsync -av --include='*/' --exclude='*' ~/Downloads ~/dls/

or

rsync -av -f"+ */" -f"- *" ~/Downloads ~/dls/

1
  • This worked perfectly. Thanks!! Oct 5 '21 at 0:55
1

mtree is a good tool for this:

$ mkdir ~/newDir
$ mtree -cdp ~/Downloads | mtree -Up ~/newDir

The first invocation of mtree creates a spec, referencing only directories, relative to the path ~/Downloads. That output gets piped to the second invocation of mtree, which Updates the relative directory paths in ~/newDir. More specifically, in this case "updates" means that any missing entries are created. Since the spec created from ~/Downloads contains only directories, only the directory structure is created.

1

rsync and mtree are great! Unfortunately, they are not always available. A bit more portable way to do it would be something like:

(cd ~/Downloads && find . -mindepth 1 -type d -print0) | (cd target_dir; xargs -0 mkdir -p)
2
  • You're right! However, both find -print0 and xargs -0 are supported on FreeBSD, Mac OS X, NetBSD, and Illumos, - not only in GNU. So it still looks quite portable to me. [AIX, HP-UX, and Oracle Solaris are out, however]. Edited my answer! Oct 6 '21 at 10:08
  • Ok, I was not aware of that. Thanks for the update!
    – AdminBee
    Oct 6 '21 at 10:09

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