Copy folder structure (without files) from one location to another

LinuxFileCopyDirectory

Linux Problem Overview


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.

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

cd /path/to/directories &&
find . -type d -exec mkdir -p -- /path/to/backup/{} \;

Solution 3 - Linux

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

Solution 4 - Linux

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

Solution 5 - Linux

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 {} \;

Solution 6 - Linux

1 line solution:

find . -type d -exec mkdir -p /path/to/copy/directory/tree/{} \;

Solution 7 - Linux

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.

Solution 8 - Linux

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

Solution 9 - Linux

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

Solution 10 - Linux

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.

Solution 11 - Linux

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 :)

Solution 12 - Linux

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:

Solution 13 - Linux

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

Solution 14 - Linux

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

Copy dir only with associated permission and ownership

Solution 15 - Linux

Simple way:

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

Solution 16 - Linux

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/{}

Solution 17 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionr00fusView Question on Stackoverflow
Solution 1 - LinuxGreg HewgillView Answer on Stackoverflow
Solution 2 - LinuxamphetamachineView Answer on Stackoverflow
Solution 3 - LinuxGildasView Answer on Stackoverflow
Solution 4 - LinuxzerodinView Answer on Stackoverflow
Solution 5 - LinuxtoliveiraView Answer on Stackoverflow
Solution 6 - LinuxIgorView Answer on Stackoverflow
Solution 7 - Linuxmijhael3000View Answer on Stackoverflow
Solution 8 - LinuxyaccobView Answer on Stackoverflow
Solution 9 - LinuxThimView Answer on Stackoverflow
Solution 10 - LinuxluissquallView Answer on Stackoverflow
Solution 11 - LinuxdotalchemyView Answer on Stackoverflow
Solution 12 - LinuxFranck DernoncourtView Answer on Stackoverflow
Solution 13 - Linuxbrother-biloView Answer on Stackoverflow
Solution 14 - LinuxTiger pengView Answer on Stackoverflow
Solution 15 - LinuxMarcos AlvesView Answer on Stackoverflow
Solution 16 - LinuxP. DView Answer on Stackoverflow
Solution 17 - LinuxChristopher K.View Answer on Stackoverflow