1752795 Members
6542 Online
108789 Solutions
New Discussion

Re: shell script

 
???_185
Regular Advisor

shell script

Hi,
My system create files everyday in specific file system.

so I want to make a script which find out specific files created in specific time
and delete it after backup to other directory.

For example, The script find out file which created from 07:00 from 21:00,
and copy that file to backup directory and delete the original.

#ls -al A
-rw-rw---- 1 ftp sys 20971520 Jul 13 09:41 a
-rw-rw---- 1 ftp sys 20971520 Jul 12 06:40 b
-rw-rw---- 1 ftp sys 20972544 Jul 11 06:20 c
-rw-rw---- 1 ftp sys 20971520 Jul 10 22:01 d
-rw-rw---- 1 ftp sys 20972544 Jul 10 21:44 e
-rw-rw---- 1 ftp sys 20972544 Jul 10 20:30 f
-rw-rw---- 1 ftp sys 20971520 Jul 10 19:20 g
-rw-rw---- 1 ftp sys 20972544 Jul 10 08:10 h
-rw-rw---- 1 ftp sys 20972544 Jul 8 21:05 i
-rw-rw---- 1 ftp sys 20971520 Jul 8 20:30 j

Can anyone help?
Thanks,
zungwon
1 REPLY 1
A. Clay Stephenson
Acclaimed Contributor

Re: shell script

The key is to use touch to create two reference files and then use the -newer option of find.

#!/usr/bin/sh


TDIR=${TMPDIR:-/var/tmp}
typeset T1=${TDIR}/Ref_1.${$}
typeset T2=${TDIR}/Ref_2.${$}
typeset BKDIR=/xxx/Backup
typeset SRCDIR=/xxx/yyy
typeset -i STAT=0
typeset FNAME=""
typeset DESTNAME=""
cd ${SRCDIR}
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "Can't cd to ${SRCDIR}" >&2
exit ${STAT}
fi
touch -t 200607090659.59 ${T1}
touch -t 200607092100.01 ${T2}
find . -type f \( -newer ${T1} -a ! -newer ${T2} \) -print | while read FNAME
do
DESTNAME="${DESTDIR}/${FNAME}"
if [[ -f "{DESTNAME}" ]]
then
echo "${DESTNAME} exists; not copied" >72
else
cp -p "{FNAME}" "${DESTNAME}"
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
rm -f "${FNAME}"
else
echo "Cp of ${FNAME} failed; status ${STAT}" >&2
fi
fi
done
rm -f ${T1} ${T2}
exit ${STAT}


man find, touch for details.
If it ain't broke, I can fix that.