Results 1 to 7 of 7

Thread: Copying directories structure without te files inside

  1. #1
    Join Date
    Dec 2008
    Location
    Montevideo, Uruguay
    Posts
    451

    Question Copying directories structure without te files inside

    Hi,

    I want to copy a full and complex directory structure into another directory but without copying any file. The *ONLY* thing I want to copy is directory structure, one the same as I created some time.

    I was reviewing the man of cp command but I didn't see anything there.

    Any other suggestion, please ?

    Thanks in advance fo any feedback,

    Regards,

    Agustin
    agunet74

  2. #2
    Join Date
    May 2012
    Location
    Finland
    Posts
    2,004

    Default Re: Copying directories structure without te files inside

    cd to the directory you want to copy the directory structure from and;

    Code:
    find ./ -type d -exec mkdir -- /destination/directory/{} \;
    So for example you want to copy the structure of /root/meow to /tmp/woof/

    Code:
    cd /root/meow
    find ./ -type d -exec mkdir -- /tmp/woof/{} \;
    Oughta do the job. Just popped into my mind
    .: miuku #suse @ irc.freenode.net
    :: miuku@opensuse.org

    .: h​ttps://download.opensuse.org/repositories/home:/Miuku/

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,171

    Default Re: Copying directories structure without te files inside

    Quote Originally Posted by Miuku View Post
    cd to the directory you want to copy the directory structure from and;

    Code:
    find ./ -type d -exec mkdir -- /destination/directory/{} \;
    So for example you want to copy the structure of /root/meow to /tmp/woof/

    Code:
    cd /root/meow
    find ./ -type d -exec mkdir -- /tmp/woof/{} \;
    Oughta do the job. Just popped into my mind
    Good idea. Are we sure that find gives the directories in hierarchical sequence? If not use mkdir -p instead of mkdir.
    Henk van Velden

  4. #4
    Join Date
    May 2012
    Location
    Finland
    Posts
    2,004

    Default Re: Copying directories structure without te files inside

    Good point hcvv, I assumed (the bad start to everything?) that find would traverse the directories in a sequential order.
    .: miuku #suse @ irc.freenode.net
    :: miuku@opensuse.org

    .: h​ttps://download.opensuse.org/repositories/home:/Miuku/

  5. #5
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,171

    Default Re: Copying directories structure without te files inside

    I did a quick test. As far as I can see it is hierarchical, thus the -p option is not needed. But it does not hurt of course.
    Henk van Velden

  6. #6

    Default Re: Copying directories structure without te files inside

    Hi,

    Code:
    source=/somewhere
    Code:
    destination=/outhere
    Code:
    find "$source" -type d -print | pax -rwdv "$destination"
    or if you don't like pax you can use cpio

    Code:
    find "$source" -type d -print | cpio -pdumv "$destination"
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  7. #7

    Default Re: Copying directories structure without te files inside

    Hi,

    you might need to cd to the source directory to avoid copying the parent directory (unless that is intended) which is done in my previous post.

    Code:
    source=/somewhere
    Code:
    destination=/outhere
    I am using a subshell ( and ) so you don't have to worry about going to the directory you are in after the command is done

    Code:
    ( cd "$source" && find . -type d -print | pax -rwdv "$destination" )


    or

    Code:
    ( cd "$source" && find . -type d -print | cpio -pdumv "$destination" )
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •