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!

Script for Yesterday 2

Status
Not open for further replies.

PaulBarbeau

Programmer
Nov 23, 2000
109
CA
Newbee question here i am trying to write a script that move the files from yesterday into a folder dated yesterday however not sure how to subtract dayone from the date. This is the script i have so far however if i run it at 11:59:59 i seam to be missing some files. Can anyone help me out?

Paul

#!/bin/ksh

date1=`date +%Y%m%d`
mkdir ${date1}
mv *${date1}* ${date1}
 
Have tried something like this:
Code:
date1=`TZ=X+24 date +%Y%m%d`

Hope This Help
PH.
 
As you use ksh, you can try this, less dependent on TimeZone:
PreviousDay(){
typeset -Z4 Y=$1; typeset -Z2 m=$2 d=$3
if [ $d -gt 1 ]; then d=$((d-1)); else
[ $m -eq 1 ] && d=31 m=12 Y=$((Y-1)) ||
m=$((m-1)) d=$(cal $m $Y | awk 'NF{d=$NF}END{print d}')
fi
echo $Y$m$d
}
date1=$(PreviousDay $(date "+%Y %m %d"))


Hope This Help
PH.
 
Hi Paul,

Your mv command moves only files which name contains the date, and you also try to move the save directory into itself.

Try something like this (move only files of the current directory) :
[tt]
date1=$(PreviousDay $(date "+%Y %m %d")) # or TZ method
[ -d ${date1} ] || mkdir ${date1}
find . \( ! -name . -prune \) -type f -exec mv {} ${date1}/
[/tt]

I don't like to create save directory in the directory to save (there are always problems to select files to move).

A simplest solution is :
[tt]
date1=$(PreviousDay $(date "+%Y %m %d"))
savdir=../${date1} # for example
[ -d ${date1} ] || mkdir ${date1}
mv * ../${savedir}/
[/tt]



Off topic: Bonsoir PH.

Jean Pierre.
 
The same without errors (i hope)
[tt]
date1=$(PreviousDay $(date "+%Y %m %d"))
savdir=../${date1} # for example
[ -d ${savdir} ] || mkdir ${savdir}
mv * ../${savdir}/
[/tt]

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top