944,125 Members | Top Members by Rank

Ad:
Oct 29th, 2007
0

newbie question regarding checking date of file

Expand Post »
Please pardon the simplicity of this question, I kind of got thrown into these responsibilities at the last minute without training.


I
I need to move a file called status10.dat to a different directory using korn scripting.

[code = korn]
cp status10.dat* /cerner/d_p19/ccluserdir
[/code]

I only want to do this if the file is equal to today's date. Is it possible to check before moving the file?

Thanks in advance
Similar Threads
jch
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jch is offline Offline
1 posts
since Oct 2007
Oct 29th, 2007
0

Re: newbie question regarding checking date of file

Hi There,

That shouldn't be a problem at all.

Depending on what you want to do you can use find to get all files created within the last 24 hours (assuming /home as your current directory):

for x in `find /home -mtime 0`
do
mv $x /cernet/d_p19/ccluserdir/.
done

you can also use the -exec option of find to do the mv for you so you'd just have a one-line find script.

Hope this helps

, Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Oct 29th, 2007
0

Re: newbie question regarding checking date of file

Hey, I just noticed that I misread your question - you need stuff from "today" and not the last 24 hours -- sorry - maybe that other info will be useful for another project.

If you want to only delete files from today, I think the simplest way to do it - to avoid the possible issues with spacing and/or tabbing would be to write a script that took a date argument in the following format:
./yourscript "Oct 3"
or
./yourscript "Oct 24"
Note that in the "Oct 3" there are two spaces separating the month and date and that "Oct 24" only has one space separating - this is to accommodate the behavious of "ls -l" -- at least, on solaris.

yourscript would be (assuming we're running it from the directory your files are in)

#!/bin/ksh

today=$1

for x in `ls -ld *`
do
filename=`echo $x|awk '{print $1}'`
result=`echo $x |grep $today >/dev/null 2>&1`
if [ $? -eq 0 ]
then
mv $filename /cernet/d_p19/ccluserdir/.
fi
done

I tend to write the short-stuff like I think. There's probably an easier way to do it

Thanks,
, Mike
Last edited by eggi; Oct 29th, 2007 at 5:14 pm. Reason: misspelling again - no more edits coming - thx :)
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Oct 30th, 2007
0

Re: newbie question regarding checking date of file

Shell Scripting Syntax (Toggle Plain Text)
  1. find /path -type f -name "status10.dat*" -mtime 0 -print0 | xargs -i -0 cp "{}" /cerner/d_p19/ccluserdir
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: call the constructor of java class from script
Next Thread in Shell Scripting Forum Timeline: dont know how to do it





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC