Copy only folders not files?
If you want to mirror a directory skeleton and copy no files:
find -type d -links 2 -exec mkdir -p "/path/to/backup/{}" \;
What's going on here:
- Find is only selecting directories
- We're using
-links 2
to find the deepest possible directories. -
mkdir -p
will make any missing directories along the way.
I've done it like this rather than find -type d -exec mkdir -p "/path/to/backup/{}" \;
because it'll cut out on a whole buttload of mkdir
calls. We can quickly prove this with a little test. Here's the test tree followed by what I ran to compare the two commands:
$ tree
.
├── dir1
│ ├── dir2
│ │ └── dir3
│ ├── dir7
│ └── dir8
└── dir9
└── dir0
$ pr -m -t <(find -type d) <(find -type d -links 2)
. ./dir1/dir8
./dir1 ./dir1/dir2/dir3
./dir1/dir8 ./dir1/dir7
./dir1/dir2 ./dir9/dir0
./dir1/dir2/dir3
./dir1/dir7
./dir9
./dir9/dir0
And that's only going to get better in a real-word solution with thousands of directories.
Quite easily done with python one-liner:
bash-4.3$ tree
.
├── ABC
├── set_pathname_icon.py
├── subdir1
│ ├── file1.abc
│ └── file2.abc
├── subdir2
│ ├── file1.abc
│ └── file2.abc
└── subdir3
└── subdir4
└── file1.txt
4 directories, 7 files
bash-4.3$ python -c 'import os,sys;dirs=[ r for r,s,f in os.walk(".") if r != "."];[os.makedirs(os.path.join(sys.argv[1],i)) for i in dirs]' ~/new_destination
bash-4.3$ tree ~/new_destination
/home/xieerqi/new_destination
├── subdir1
├── subdir2
└── subdir3
└── subdir4
As script this could be rewritten like so:
#!/usr/bin/env python
import os,sys
dirs=[ r for r,s,f in os.walk(".") if r != "."]
for i in dirs:
os.makedirs(os.path.join(sys.argv[1],i))