Moving Old Files with Directory Structure in a Large Directory

How to Move Old Files with Directory Structure in a Large Directory

by Ross Moffatt, September 2008

This tech tip shows how to move all files older that a set number of days ago to a new (existing) directory, keeping the original directory structure.  The old directory structure is left in place. This script works if there are 200000 odd files in total in the directory structure where you may get the dreaded "Arg list too long" type of error.

Note: This procedure works on Solaris versions up to and including Solaris 9. It should work on systems that run the Solaris 10 OS, but that was not tested.

Basic Commands

Here are the basic commands, the first find/cpio command copies the selected (older than 30 days) files including the directory structure.  The second command deletes the same files from the original directory structure leaving the directories in place.

cd <source directory>
find . -type f -mtime +30 | cpio -pd <destination directory>
find . -type f -mtime +30 -exec rm -f {} \;
A Simple Script
#!/bin/sh
# by Ross, ross.stuff@telstra.com
# Setting Source, Destination Folders, and how many days to keep files in place.
ORIGPATH=`pwd`
SRCPATH="/var/ross/current"
DSTPATH="/var/ross/archive"
RTNDAYS=30
#
cd $SRCPATH
find . -type f -mtime +$RTNDAYS | cpio -pmad $DSTPATH
find . -type f -mtime +$RTNDAYS -exec rm -f {} \;
cd $ORIGPATH
#
exit 0
About the Author

Ross Moffatt has been a UNIX system administrator for 10+ years and can be contacted at ross.stuff@telstra.com.


Labels

bigadmin bigadmin Delete
sysadmin sysadmin Delete
files files Delete
move move Delete
delete delete Delete
directory directory Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Oct 11, 2008

    sdckdo says:

    Ross, while I'm using a similar procedure once in a while, I wasn't aware of th...

    Ross,

    while I'm using a similar procedure once in a while, I wasn't aware of the -a flag to cpio - that's really useful.

    But I suggest two modifications: the -depth flag to find will handle a directory after its entries and will thus allow cpio to restore the original creation time. It would also be useful to add the -l flag to cpio: if both your source tree and your archive are on the same filesystem, this will save you the physical copy.

    The nice thing with find and cpio is that you can easily filter out files, which need not be archived.

    Thank you, and have a nice day,

    Klaus

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact