LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-23-2004, 01:13 PM   #1
ronin1
LQ Newbie
 
Registered: Jun 2004
Posts: 8

Rep: Reputation: 0
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.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 07-23-2004, 05:36 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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"
 
Old 07-23-2004, 06:55 PM   #3
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,304

Rep: Reputation: 65
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.
 
2 members found this post helpful.
Old 07-23-2004, 09:49 PM   #4
afm
LQ Newbie
 
Registered: Jul 2004
Posts: 16

Rep: Reputation: 2
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
 
3 members found this post helpful.
Old 08-03-2004, 02:39 PM   #5
ronin1
LQ Newbie
 
Registered: Jun 2004
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks everyone for your help
 
Old 03-23-2013, 04:06 PM   #6
vineel567
LQ Newbie
 
Registered: Oct 2012
Posts: 1

Rep: Reputation: Disabled
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
 
Old 03-26-2013, 12:47 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Edit: 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.

Last edited by David the H.; 03-26-2013 at 01:03 PM. Reason: re-worded & expanded re: necro-threading
 
Old 03-27-2013, 10:35 AM   #8
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
How about this?
Code:
$ find -type d -exec mkdir -p /target-folder/{} \;

Last edited by Madhu Desai; 03-28-2013 at 02:19 PM.
 
Old 03-27-2013, 11:48 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 06-08-2013, 06:16 AM   #10
Nutria
Member
 
Registered: Nov 2007
Location: New Orleans, LA, USA
Distribution: Xubuntu
Posts: 67

Rep: Reputation: 6
Unhappy

All these examples are great, but they don't carry over ownership/group or permissions.
 
Old 06-08-2013, 06:23 AM   #11
Nutria
Member
 
Registered: Nov 2007
Location: New Orleans, LA, USA
Distribution: Xubuntu
Posts: 67

Rep: Reputation: 6
Quote:
Originally Posted by ronin1 View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to copy an entire directory structure except certain files? thanhvn Programming 9 01-27-2012 11:41 AM
Directory Structure fedix Linux - Newbie 3 10-14-2005 02:48 PM
Copy directory structure? tpe Programming 2 06-02-2005 04:59 AM
"WARNING: Circular directory structure" error when deleting directory pistonbrew Linux - Software 5 02-03-2005 06:05 AM
directory structure sluggo Linux - Newbie 1 01-20-2002 08:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration