152

I want to create a clone of the structure of our multi-terabyte file server. I know that cp --parents can move a file and it's parent structure, but is there any way to copy the directory structure intact?

I want to copy to a linux system and our file server is CIFS mounted there.

3
  • 4
    Possible duplicate of Rsync how to include directories but not files? Commented Jun 1, 2017 at 20:14
  • rsync solution from the above comment post looks neater.
    – Y. E.
    Commented Sep 28, 2018 at 7:41
  • I just noticed if you use rsync's --max-size=<some tiny number>, you can get a lot of the structure copied. Ok you'll get tiny files too but I think I prefer this solution to using some plaintext hack. Commented May 28 at 22:03

18 Answers 18

248

You could do something like:

find . -type d > dirs.txt

to create the list of directories, then

xargs mkdir -p < dirs.txt

to create the directories on the destination.

11
  • 17
    This solutions won't work if you have spaces in your directory names.
    – Jealie
    Commented Mar 11, 2015 at 20:35
  • 51
    @Jealie Change the commands to find . -type d -print0 >dirs.txt and xargs -0 mkdir -p <dirs.txt. This will cause both commands to use nulls as separators instead of whitespace. Commented Apr 3, 2015 at 1:37
  • 8
    xargs can exceed the maximum command length of the system when you start dealing with orders of hundreds or thousands, so use this with caution. (Find the command length limit with getconf ARG_MAX.) With a lot of directories, you may have to write a script to loop through the output instead.
    – palswim
    Commented Oct 13, 2015 at 21:26
  • 4
    And what about permissions & attributes will it be retained ?? Commented Aug 17, 2016 at 7:14
  • 4
    @TrevorBoydSmith: Thanks for your comment. The original question didn't mention anything about needing to preserve permissions, ownership, or attributes. Doing so would require a different solution, as you mention, but the above is sufficient to answer the question as posed. Commented Jun 1, 2017 at 20:16
103
cd /path/to/directories &&
find . -type d -exec mkdir -p -- /path/to/backup/{} \;
4
  • 4
    Best answer from me with find. Else you can try rsync solution from Chen Levy answer in this SO question
    – Mat M
    Commented May 14, 2014 at 13:14
  • 3
    What does the -- mkdir's option? Commented Aug 8, 2016 at 8:58
  • 12
    -- is a standard GNU utility option terminator. It means "hey mkdir, any argument after this, it's not a flag, so treat it as a file argument, even if it starts with a '-' character." Commented Aug 9, 2016 at 16:35
  • 6
    this answer also does not retain the directory permissions and attributes. Use rynsc to preserve the permissions and attributes Commented Jun 1, 2017 at 20:13
69

Here is a simple solution using rsync:

rsync -av -f"+ */" -f"- *" "$source" "$target"
  • one line
  • no problems with spaces
  • preserve permissions

I found this solution there

4
  • 21
    same but more readable rsync -a --include '*/' --exclude '*' "$source" "$target"
    – Sylvain
    Commented Mar 30, 2018 at 9:16
  • 3
    Problem with this solution is: my folders contain thousands of files and rsync takes ages just to sync a dozen of folders. Commented Jul 24, 2018 at 19:11
  • 1
    Very nice! I used it with recursively with -r. In my experience rsync is fast as long it does not work on network file systems. In that case better use it's integrated ssh.
    – Hauke
    Commented Jul 19, 2023 at 14:49
  • Any idea how to exclude hidden directories?
    – dastrobu
    Commented Dec 14, 2023 at 13:23
14

1 line solution:

find . -type d -exec mkdir -p /path/to/copy/directory/tree/{} \;
1
  • 2
    This works fine and works smartly. One wrinkle ylu must be in the $source directory to ensure your new tree is relative to the $target directory. But that's not one-line any more. Use: pushd $source; find . -type d -exec mkdir -p "$target"/{} \; popd
    – will
    Commented Mar 27, 2022 at 12:50
10

I dunno if you are looking for a solution on Linux. If so, you can try this:

$ mkdir destdir
$ cd sourcedir
$ find . -type d | cpio -pdvm destdir
3
  • cpio doesn't seem to work for me, at least with the parameters you specified.
    – r00fus
    Commented Nov 8, 2010 at 23:02
  • @r00fus - please read the manual for cpio or refer gnu.org/software/cpio
    – zerodin
    Commented Nov 8, 2010 at 23:10
  • Could you please tell me what does -pdvm do? I tried the man cpio command but no avail. Commented Oct 10, 2022 at 11:38
9

This copy the directories and files attributes, but not the files data:

cp -R --attributes-only SOURCE DEST

Then you can delete the files attributes if you are not interested in them:

find DEST -type f -exec rm {} \;
3
  • 1
    Would be exellent one, but you forgot to mention saving ownership, timestamp and permissions. So it produced a mess in Win7/cygwin - NULL_SID user, wrong permissions order, cannot edit permissions, etc and cannot access produced filestructure.
    – WebComer
    Commented Feb 23, 2018 at 1:37
  • 3
    I should think the blame lies squarely on whoever tries to use Windows for real work.
    – tripleee
    Commented Feb 23, 2019 at 9:10
  • 1
    cp -R --attributes-only --preserve=all --parents -v SOURCE DEST
    – Gábor
    Commented May 7, 2020 at 15:08
3

This works:

find ./<SOURCE_DIR>/ -type d | sed 's/\.\/<SOURCE_DIR>//g' | xargs -I {} mkdir -p <DEST_DIR>"/{}"

Just replace SOURCE_DIR and DEST_DIR.

2

The following solution worked well for me in various environments:

sourceDir="some/directory"
targetDir="any/other/directory"

find "$sourceDir" -type d | sed -e "s?$sourceDir?$targetDir?" | xargs mkdir -p
2

This solves even the problem with whitespaces:

In the original/source dir:

find . -type d -exec echo "'{}'" \; > dirs2.txt

then recreate it in the newly created dir:

mkdir -p <../<SOURCEDIR>/dirs2.txt
2

Another tool that can do this: mtree

It first appeared on bsd systems, there are ports for linux too. In Ubuntu it is in package 'mtree-netbsd' or 'freebsd-utils'.

# 1: cd to your source dir
cd my_source_dir

# 2: create a specfile. Place the spec outside of the sourcedir and destdir
mtree -c > /tmp/myspec.txt

# 3: cd to the destdir
cd my_dest_dir

# 4: now create only the dir structure from the specfile
# -u  is for creating only dirs, it does not follow links
mtree -f /tmp/myspec.txt -u

If you later deleted some dirs in the destdir call the command form step 4 again, dir-structure will be restored.

mtree can do a lot more. follow links, create/delete specific files/dirs, delete or not delete files that are in the destdir...

1

If you can get access from a Windows machine, you can use xcopy with /T and /E to copy just the folder structure (the /E includes empty folders)

http://ss64.com/nt/xcopy.html

[EDIT!]

This one uses rsync to recreate the directory structure but without the files. http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html

Might actually be better :)

2
  • 1
    Unfortunately, our CIFS-serving fileserver isn't running windows, so no can do on win commands.
    – r00fus
    Commented Nov 8, 2010 at 23:04
  • Thank you, the rsync method worked perfectly fine for me. It's compatible with spaces in directory names as well. Commented Dec 7, 2012 at 0:40
1

Substitute target_dir and source_dir with the appropriate values:

cd target_dir && (cd source_dir; find . -type d ! -name .) | xargs -i mkdir -p "{}"

Tested on OSX+Ubuntu.

1
find source/ -type f  | rsync -a --exclude-from - source/ target/

Copy dir only with associated permission and ownership

1
cd oldlocation
find . -type d -print0 | xargs -0 -I{} mkdir -p newlocation/{}

You can also create top directories only:

cd oldlocation
find . -maxdepth 1 -type d -print0 | xargs -0 -I{} mkdir -p newlocation/{}
1
  • This solution works perfectly on LinuxMint 20.2, unlike the answer validated 10 years ago. Thank you!
    – user2456016
    Commented Nov 8, 2021 at 10:22
0

A python script from Sergiy Kolodyazhnyy posted on Copy only folders not files?:

#!/usr/bin/env python
import os,sys
dirs=[ r for r,s,f in os.walk(".") if r != "."]
for i in dirs:
    os.makedirs(os.path.join(sys.argv[1],i)) 

or from the shell:

python -c 'import os,sys;dirs=[ r for r,s,f in os.walk(".") if r != "."];[os.makedirs(os.path.join(sys.argv[1],i)) for i in dirs]' ~/new_destination

FYI:

0

Another approach is use the tree which is pretty handy and navigating directory trees based on its strong options. There are options for directory only, exclude empty directories, exclude names with pattern, include only names with pattern, etc. Check out man tree

Advantage: you can edit or review the list, or if you do a lot of scripting and create a batch of empty directories frequently

Approach: create a list of directories using tree, use that list as an arguments input to mkdir

tree -dfi --noreport > some_dir_file.txt

-dfi lists only directories, prints full path for each name, makes tree not print the indentation lines,

--noreport Omits printing of the file and directory report at the end of the tree listing, just to make the output file not contain any fluff

Then go to the destination where you want the empty directories and execute

xargs mkdir < some_dir_file.txt
0

Simple way:

for i in `find . -type d`; do mkdir /home/exemplo/$i; done
-1

Here is a solution in php that:

  • copies the directories (not recursively, only one level)
  • preserves permissions
  • unlike the rsync solution, is fast even with directories containing thousands of files as it does not even go into the folders
  • has no problems with spaces
  • should be easy to read and adjust

Create a file like syncDirs.php with this content:

<?php
foreach (new DirectoryIterator($argv[1]) as $f) {
    if($f->isDot() || !$f->isDir()) continue;
        mkdir($argv[2].'/'.$f->getFilename(), $f->getPerms());
        chown($argv[2].'/'.$f->getFilename(), $f->getOwner());
        chgrp($argv[2].'/'.$f->getFilename(), $f->getGroup());
}

Run it as user that has enough rights:

sudo php syncDirs.php /var/source /var/destination

1
  • 1
    You don't have to like PHP and you don't have to use it. But the OP did not specify whether he wants a solution in any specific language and, like it or not, PHP is installed anyways on lots of Linux systems as 80% of the web uses PHP ( w3techs.com/technologies/details/pl-php/all/all ). Commented Aug 21, 2019 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.