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

I want to create a clone of the structure of our multi-interval file server. I know that cp -parents can move the file and its parent structure, but is there a way to copy the directory structure without changes?

I want to copy to a Linux system, and our file server has CIFS installed there.

+72
linux file directory copy folders
Nov 01 '10 at 23:30
source share
13 answers

You can do something like:

find . -type d >dirs.txt 

to create a list of directories, then

 xargs mkdir -p <dirs.txt 

to create directories at destination.

+132
Nov 01 '10 at 23:37
source share
 cd /path/to/directories && find . -type d -exec mkdir -p -- /path/to/backup/{} \; 
+66
Nov 01 '10 at 23:39
source share

Here is a simple solution using rsync:

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

I found this solution there

+20
Aug 30 '17 at 12:07 on
source share

I do not know if you are looking for a solution for Linux. If so, you can try the following:

 $ mkdir destdir $ cd sourcedir $ find . -type d | cpio -pdvm destdir 
+6
Nov 01 '10 at 23:41
source share

Copy the attributes of directories and files, but not files:

 cp -R --attributes-only SOURCE DEST 

You can then remove the file attributes if they are not of interest to you:

 find DEST -type f -exec rm {} \; 
+4
Dec 27 '15 at 17:15
source share

It 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
Jun 04 '15 at 14:20
source share

Substituting target_dir and source_dir with the corresponding values:

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

Tested on OSX + Ubuntu.

+1
Aug 31 '12 at 23:59
source share

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 
+1
Jul 22 '13 at 16:07 on
source share

This even solves the problem with spaces:

In source directory / source:

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

then recreate it in the newly created directory:

 mkdir -p <../<SOURCEDIR>/dirs2.txt 
+1
Dec 18 '17 at 13:32
source share

Python script from Sergey Kolodyazhny 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
Aug 08 '17 at 23:13
source share

Here is the solution in php which:

  • copies directories (not recursively, only one level)
  • saves permissions
  • unlike rsync, it works quickly even with directories containing thousands of files, since it does not even end up in folders
  • no problems with spaces
  • should be easy to read and tune

Create a file as 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 a user with sufficient rights:

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

0
Jul 25 '18 at 18:02
source share

Another approach is to use a fairly convenient tree and navigate directory trees based on its powerful options. There are options for a directory only that exclude empty directories, exclude names with a picture, include only names with a picture, etc. Check man tree

Advantage : you can edit or browse the list, or if you make a lot of scripts and often create a package of empty directories

Approach : create a list of directories using tree , use this list as an argument for input to mkdir

 tree -dfi --noreport > some_dir_file.txt 

-dfi lists only directories, prints the full path for each name, causes the tree not to print indent lines,

--noreport Allows you to print a file and directory report at the end of the tree list, just so that the output file does not contain fluff

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

 xargs mkdir < some_dir_file.txt 
0
May 13 '19 at 20:14
source share

If you can access from a Windows computer, you can use xcopy with / T and / E to copy only the folder structure (/ 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

Perhaps it will be better :)

-one
Nov 01 '10 at 23:33
source share



All Articles