Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

moving files by dates

Status
Not open for further replies.

hpoko

IS-IT--Management
Jan 24, 2003
4
US
Need some help with being able to move older files from one
directory to another. The system writes files to /tmp and I would like to select the files by date and move them to another files system.

If I did an ls -lta and had the 3 files below and I needed to move the files that were created before 2001 from /tmp to /data/hist/

-rw-rw---- 1 root system 700 23 Jan 24 19:20 testfile
-rw-rw---- 1 root system 150 11 Aug 18 2001 faux
-rw-rw---- 1 root system 800 20 Aug 18 2001 text

How can I do this?

Thank you for your help!
 
Hi,
you can use scripts like this
#!/bin/ksh
OldDir=`pwd`
cd /tmp
ls -la |&
while read -p fl
do
set `echo "A$fl"`
if [ $# -eq 9 ]
then
dt=$8
fn=$9
if [ ${#dt} -eq 4 ]
then
if [ $dt -le 2001 ]
then
mv $fn /data/hist
fi
fi
fi
done
cd $OldDir
Regards Boris
 
You can use backup/restore to do it.
# cd <directorytomove>
<directorytomove> # find . -mtime +365 | backup -ivf /tmp/copy.of.files
<directorytomove> # cd <targetdirectory>
<targetdirectory> # restore -xvf /tmp/copy.of.files

In this example I make backup of those files which modification time is more than a year. Apply it to your needs.

GoodBye!
 
And (I forgot it) remove the files from original location with
<directorytomove> # find . -mtime +365 -exec rm {} \;

Regards!

 
Thanks Boria and swtbart for you help. I finally had the chance to try it. Boria your script works great but not sure what it all means.

The commands from swtbart works great too, but I am not sure what I am doing wrong on the last line I cannot get it to delete the files from the old dir.

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top