APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Cloning a directory structure without copying files

I'm always somewhat amused when I go searching for help and Google sends me to my own site for the answer. That's what happened here when I needed to duplicate a directory structure without copying certain files. The solution given at the link referenced above wasn't quite what I wanted, though it did put me on the right track.

I needed to clone user directories on an SME server without copying their mail message files. The reason for this was that we wanted to do a Desktop Backup for a complicated upgrade, which in the SME world means creating a backup and saving it on a Windows box. As these people used IMAP, the user directories were chock full of mail messages and the total size of the backup would be far too large for Windows to accept over an SMB connection.

SME's desktop backup is script driven, so we could have hacked the script to do what we wanted, but that probably would have been fairly difficult.

The script from the referenced link wasn't quite what was needed, because other than the actual mail messages, we wanted to backup any other ordinary files found. Since the mail files are easily matched by a wildcard pattern, this wasn't hard:

# stop mail services
svc -d /service/smtpfront-qmail
svd -d /service/qmail
mkdir /dirclone
cd /home/e-smith/files/users
# now copy the directory, leaving out the *e-smith* files
for i in *
 rsync -v -rolgpt --exclude="*e-smith*"  $i /dirclone 
cd ..
mv users usersall
mv /dirclone users
# do backup, then reverse the directory renaming and restart services

That duplicates the directory hierarchy but leaves out "*e-smith*" files, which is what we want. Rsync does all the work of copying and creating directories with the proper ownership and permissions while conveniently allowing us to say what we DON'T want copied. Note that the exclusion is a wild-card format, not a regex. The rest of the rsync flags make sure that links, permissions, ownerships and time stamps are preserved and that rsync gives us feedback as it descends into the directories.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Cloning a directory structure without copyingfiles

Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Tony Lawrence

Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us