LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Copy Directory Structure Only (http://www.linuxquestions.org/questions/linux-newbie-8/copy-directory-structure-only-208796/)

ronin1 07-23-2004 02:13 PM

Copy Directory Structure Only
 
Does anyone know a command that allows one to copy a directory structure to another location. The structure is very deep and I do not need the files in any of the directories.

Dark_Helmet 07-23-2004 06:36 PM

I don't know if there's a single command to do that... If there is, it might be some form of the find command. Here's a script that will work for you though.
Code:

#!/bin/bash

old_ifs=${IFS}
IFS=$'\n'

for directory in $( find . -type d ); do
  new_directory=$( echo ${directory} | sed "s%\./%placeholder/%" )
  echo "mkdir -p \"${new_directory}\""
  #mkdir -p "${new_directory}"
done

IFS=${old_ifs}

exit 0

A few things to note:
1. You must be in the top-level directory of the structure you wish to copy when you run the script.

2. You need to change the placeholder text in the script to be the directory you copy the directory structure to. You can use an absolute pathname if you like, but no matter what, it needs to have the trailing slash (which is why it's not listed in red).

3. The script will not actually copy the structure until you remove the # character in front of the mkdir command above. This is a safeguard so you can actually see the commands the script will execute before you "cut it loose"

whansard 07-23-2004 07:55 PM

find . -type d -exec mkdir /destination/{} \;

example as run by me.
find . -type d -exec mkdir /work/work/{} \;


does it all, but copies hidden files in top directory only. i can't figure out why. but it's pretty close.
cd /work/work; rm .*
deletes the hidden files in the top.. maybe i'll figure it out later.

afm 07-23-2004 10:49 PM

Re: Copy Directory Structure Only
 
Quote:

Originally posted by ronin1
Does anyone know a command that allows one to copy a directory structure to another location. The structure is very deep and I do not need the files in any of the directories.
Code:

# cd /new/dir
# (cd /old/dir; find -type d ! -name .) | xargs mkdir


ronin1 08-03-2004 03:39 PM

Thanks everyone for your help

vineel567 03-23-2013 05:06 PM

A small change to handle folder names with spaces

# cd /new/dir
# (cd /old/dir; find -type d ! -name . -printf "\"%p\"\n") | xargs mkdir




"\"%p\"\n" Double quoted path names or else xargs and mkdir will fail

David the H. 03-26-2013 01:47 PM

Edit: :doh: this is a resurrected necro-thread.

@vineel567, please indicate when you are re-opening an old thread so that readers don't confuse it with something being currently discussed. And don't re-open them at all unless you have something important to add to that discussion, such as a correction or updated info.

Your current post is fine in that regard, but I would recommend not trying to include hard-coded quotemarks in a command. Instead, use null separators in find and xargs.

I'd also recommend using the -p option in mkdir to avoid error messages, and perhaps find's -depth option (although that's not really necessary).

Finally, you shouldn't assume that find will operate on the PWD. Always explicitly give the directories it should start searching from.

Code:

cd /new/dir
( cd /old/dir ; find . -depth -type d ! -name . -print0 ) | xargs -0 mkdir -p


Another addition of my own, by the way:
How can I recreate a directory hierarchy structure, without the files?
http://mywiki.wooledge.org/BashFAQ/010


And please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

mddesai 03-27-2013 11:35 AM

How about this?
Code:

$ find -type d -exec mkdir -p /target-folder/{} \;

colucix 03-27-2013 12:48 PM

Resurrected is resurrected so... here is another way using rsync:
Code:

rsync -a --include '*/' --exclude '*' /path/to/source user@host:
or
Code:

rsync -a --include '*/' --exclude '*' /path/to/source /path/to/dest
for local copies. Don't forget to add the -v and the --dry-run options of rsync for testing.

Nutria 06-08-2013 07:16 AM

All these examples are great, but they don't carry over ownership/group or permissions.

Nutria 06-08-2013 07:23 AM

Quote:

Originally Posted by ronin1 (Post 1065333)
Does anyone know a command that allows one to copy a directory structure to another location. The structure is very deep and I do not need the files in any of the directories.

This will retain ownership and group. Probably other privs, too.
Code:

find . -type d | cpio -pdvm destdir


All times are GMT -5. The time now is 07:44 AM.