![]()
|
SysAdmin Topics
|
How to Move Old Files with Directory Structure in a Large Directoryby 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 CommandsHere 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 AuthorRoss Moffatt has been a UNIX system administrator for 10+ years and can be contacted at ross.stuff@telstra.com. |
Comments (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