0

I got requirement to create mirror image of existing Linux directories and sub directories of folders as below.

we have "/opt/current" directory which has another layer of 20 sub directories and one more layer with 50 sub directories and so on...few more deeper layer for sub-directories and total we have 1000 directories from parent to child. the /opt/current is mounted on Oracle ZFS appliance.

How to create exact mirror directories/sub-directories structure in new location /opt/New not manually like one to one directory and apply permissions. the /opt/New is mounted on another ZFS appliance.

Is there a way/script to create entire directory structure from parent to child with same owner/group/other permission in /opt/New path as in /opt/current path?

thanks, CG

5 Answers 5

2
rsync -av --include='*/' --exclude='*' /opt/current/ /opt/New

This would use rsync to exactly duplicate the directory structure of /opt/local in /opt/New while avoiding copying any files.

The -a option (--archive) would make sure that all permissions etc. are preserved (this option also implies a recursive copy), and -v (--verbose) would be giving you an indication of the progress of the command.

With --include='*/' we explicitly include any directory in the transfer, but exclude everything else with --exclude='*'. The ordering of these to options on the command line is important.

The / at the end of /opt/current/ is also important, as without it, the result would be to create /opt/New/current instead of writing into /opt/New directly.

1
  • thank you.. i will test this.
    – user383043
    Nov 22, 2019 at 20:08
1

POSIXly and assuming directory names don't contain newline characters, and $srcdir and $dstdir are absolute paths:

(cd -P "$srcdir" && find . -type d | pax -rwdpe "$dstdir/")

Some pax implementations support getting NUL-delimited lists of files to copy with a -0 option to allow filenames with NULs:

(cd -P "$srcdir" && find . -type d -print0 | pax -0rwdpe "$dstdir/")

(-print0 is also a non-POSIX extension).

0

Alternatively to rsync

find /opt/current -type d -print0 | cpio -0pma backup/dir/
0

mtree is designed for just this sort of task.

# mkdir /opt/New
# mtree -cdp /opt/current | mtree -Up /opt/New

FMI, see this cross-site duplicate.

-2

You can make a script like the following... put that in a file:

-- bash script/ksh script syntax will need to change depending on shell ----

find . -name "*"|while read line
do
        if [[ -d ${line} ]]
        then 
                echo ${line}
        fi
do

on server you are making the file

cat file |while read line
do
mkdir ${line}
done
2
  • 1
    Note that -name "*" will match anything, so it can be deleted. You may also use -type d with find instead of the [[ -d $line ]] test. Also note that read strips flanking whitespace off from the strings that it reads by default, and interprets backslash under some circumstances, so you may want to use IFS= read -r line instead. But using -type d with find would get rid of that loop anyway. The mkdir bit would unfortunately not preserve the meta data of the original directories, which was one thing that the user wanted to do.
    – Kusalananda
    Nov 22, 2019 at 18:36
  • yep.. i was just giving a simple "quick and easy" thing.. There are about 10k ways to do this. Given that the user wants to replicate the directory structure only, I wouldn't do anything "rm: ... I would simply make a report of the directory structure on one box, then duplicate that on the other... your "find" command would absolutely work much the same way dumping that info to a file for sure.
    – nocool
    Nov 22, 2019 at 18:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .