How do I copy a directory tree but not the files in Linux?

19,675

Solution 1

Just found this:

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

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

Solution 2

find some/dir -type d -print0 | rsync --files-from=/dev/stdin -0 ...

Solution 3

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.

Solution 4

You also can do :

find inputdir -type d | cpio -pdumv destdir

The power of simplicity ;)

Solution 5

(cd /home/user/source/; find -type d -print0) | xargs -0 mkdir -p
Share:
19,675

Related videos on Youtube

Kyle West
Author by

Kyle West

entrepreneur, designer, developer.

Updated on September 17, 2022

Comments

  • Kyle West
    Kyle West almost 2 years

    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.

  • Manos Vajasan
    Manos Vajasan about 8 years
    It is not possible to create hard links across filesystem boundaries
  • SvennD
    SvennD about 8 years
    True, but it wasn't said that it was across filesystem ...
  • Trevor Boyd Smith
    Trevor Boyd Smith about 7 years
    I prefer this syntax rsync -a --include='*/' --exclude='*' ${source} ${destination}.
  • Law29
    Law29 over 5 years
    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.
  • Yaroslav Nikitenko
    Yaroslav Nikitenko over 3 years
    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
    Yaroslav Nikitenko over 3 years
    It would be useful to add what is -0 and what is the ellipsis. The former, I guess, is for rsync to read the output of find's print0.