20

I want to copy about 200 directories & subdirectories from one location to another but I don't want to copy the thousands of files within those directories. I am on Linux.

Note: I don't have enough space to copy everything then delete all the files.

Zypher
  • 36,995
  • 5
  • 52
  • 95
Kyle West
  • 636
  • 1
  • 6
  • 11

8 Answers8

27

Just found this:

rsync -a -f"+ */" -f"- *" source/ destination/

http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html

Kyle West
  • 636
  • 1
  • 6
  • 11
9
find some/dir -type d -print0 | rsync --files-from=/dev/stdin -0 ...
Ignacio Vazquez-Abrams
  • 45,019
  • 5
  • 78
  • 84
6

Another approach is with find and mkdir:

find SOURCE -type d -exec mkdir TARGET/{} \;

Just make sure TARGET already exists or use the -p option of mkdir.

3

You also can do :

find inputdir -type d | cpio -pdumv destdir

The power of simplicity ;)

Pilou
  • 33
  • 2
  • In the beginning of `man cpio` it says: "\_\_WARNING\_\_ The cpio utility is considered LEGACY based on POSIX specification. Users are encouraged to use other archiving tools for archive creation." – Yaroslav Nikitenko Nov 04 '20 at 18:08
1

Similarly, using (GNU) tar:

find some/dir -type d -print |
tar --no-recursion -T- -c -p -f- |
(cd another/dir && tar -x -p -f-)

You don't really need the -print0 on the find command line or the -0 on the rsync command line unless you have filenames that contain newline characters (which is possible but highly unlikely). Tar (and rsync, and cpio) read filenames line-by-line; using a NULL terminator is mostly useful with xargs, which normally reads whitespace separated filenames (and so does not handle files/directories with spaces in their names without -0).

Pilou
  • 33
  • 2
larsks
  • 41,276
  • 13
  • 117
  • 170
1
(cd /home/user/source/; find -type d -print0) | xargs -0 mkdir -p
SergioAraujo
  • 109
  • 3
-1
cp -al 

Would copy all files with hard links. Same structure, same permissions. (note: hard links, so no storage lost.)

SvennD
  • 739
  • 5
  • 18
-1

ls -d */ @source: find . -type d -print0 >dirs.txt @destination: xargs -0 mkdir -p

This will cause both commands to use nulls as separators instead of whitespaces. Note that the order of -type d and -print0 is important!

Ashish Karpe
  • 277
  • 2
  • 5
  • 19
  • This is not clear and seems incorrect. It seems that the `@source` and `@destination` are indications to the reader, but even so this cannot work. – Law29 Oct 02 '18 at 05:24