Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have to calculate the date - 1. I use this shell:
#!/bin/sh
DataFlusso='date +"%Y%m%d"'
DataFlusso='expr $DataFlusso - 1'
but if the date is 20040501 return 20040500.
Someone can help me?
Thanks!!
You can get yesterday's date with:
TZ=aaa24 date +%Y%m%d
Or if you are using GNU date, you can do:
date --date=yesterday
Report Offensive Follow Up For Removal
Hi Fdipaolo,
I am having the same date problem, I need to find all files with a date greater than or equal to Jan 1, 2004.
I do not know how to go back to jan 1, 2004, can anyone help....! please!
camin
Report Offensive Follow Up For Removal
I have an other question: TZ is the date a local zone. But I have to put the return value of TZ=aaa24 date +%Y%m%d
into a variable DataFlusso. But if I make the assignment
DataFlusso = $TZ
DataFlusso is blank. Why?
Two functions I wrote long ago that play around with dates for systems the do not have the GNU date command. Feel free to modify them and incorporate them into your scripts.
######################
function leapCheck { #
######################
# Comments: This function was written to determine if Feb ends on the 28th or
# on the 29th. It was part of the 24 hour review of errors from the 'errpt'
# command. It is not being implemented at this time but the function was left
# in for future implementation.
#######################for LastDay in $(cal 02 $(date "+%Y") )
do
:
done
export LastDay} # end of the function leapCheck.
This function calculates the dates:
#########################
function getStartTime { #
#########################
# Comments: Function orginally written to allow a search back 24 hours in the
# error log. Function is not implemented but is being left in for possible
# future implementation.
#########################Month=$(date "+%m")
if [[ $(date "+%d") = 1 ]]
then
if [[ $(date "+%m") = 1 ]]
then
(( Year=$(date "+%y") - 1 ))
Month=12
Day=31
else
(( Month=$Month - 1 ))
case $Month in03 | 06 | 09 | 11 ) LastDay=30
;;
02 ) :
;;
* ) LastDay=31
;;
esac
Year=$(date "+%y")
(( Month=$(date "+%m") - 1 ))
Day=$LastDay
fi
else
Year=$(date "+%y")
(( Day=$(date "+%d") -1 ))
fiStartTime=$Month$Day$(date "+%H%M")$Year
} # end of the getStartTime function
Report Offensive Follow Up For Removal
Actually camin, your problem is nothing like fdipaolo's, thus it should really start a new thread.
fdipaolo wanted simply to get the date from the system and subtract one day from it. You want to find files based on a given date.
Here is a simple solution, but it comes with a slight flaw:
touch -t200401010000 /tmp/Jan01
find /mypath -type f -newer /tmp/Jan01The above will find files newer than the control file that I created named Jan01, and which has a modify date of Jan 01 00:00.
The flaw is that the touch command allows me to specify my desired date stamp only to the nearest minute (00:00) and it defaults to 00:00:00. Since the find command is looking for files newer than this, it will not select files that have a date stamp of exactly Jan 01 00:00:00. It will
select files having a date stamp of Jan 01 00:00:01 and higher.The find command will find files that are "newer than" or "not newer than", when what we really need here is "not older than", which would be equivalent to "equal to or newer than".
To avoid missing that one-second window, we could instead do this:
touch -t200312312359 /tmp/Dec31
find /mydirtree -type f -newer /tmp/Dec31That solution will select files that are newer than Dec 31 23:59:00 which means that it would also select files in the 23:59:01 to 23:59:59 range. So instead of missing files in a one-second window, this solution will give us files in a 59-second window that are too old.
I am not good at perl, but I do know that this would be an easy task for perl. There may be a straight-forward way to code it, but if not, I know that perl could pull each file's mtime (number of seconds since Jan 1, 1970):
@file_info = stat("myfile");
print $file_info[9],"\n";The perl script would want to select files having mtime seconds of 1072936800 or greater, which is exactly Jan 01, 2004 00:00:00.
Maybe someone will post the perl solution.
Report Offensive Follow Up For Removal
A million thanks Jim B.
I will try your suggestion....will let you know.
camin
Report Offensive Follow Up For Removal
Hi Jim,
I tried using [touch -t200312312359 /tmp/Dec31 ]
but I get error: bad time specification
thanks for all your help..I do not know perl. Any other suggestions would be great...
camin
Report Offensive Follow Up For Removal
camin, what operating system are you using, and what shell?
uname -a
echo $SHELL
Report Offensive Follow Up For Removal
Hi Jim,
I am using SunOS 5.6 Generic_105181-38 sun4u sparc SUNW,Ultra-60
SHELL=/bin/csh
thanks.
camin
Report Offensive Follow Up For Removal
Well, I don't know why it does not like the time specification. You might try looking at the man pages (man touch). Maybe your version requires accuracy to the second instead of to the minute (which would be good). If that were the case, you would want:
touch -t20031231235959 /tmp/Dec31
Report Offensive Follow Up For Removal
Hi Jim,
I have looked up touch in the man pages,
I came up with this:
touch -t 2003123123.59 /tmp/Dec31
find /mydirtree -type f -newer /tmp/Dec31am I right to say that the first statement will create a file called Dec31 with this 2003123123.59 in it? yes/no?
and the second statement will find all files newer than that date specified in /tmp/Dec31?
yes/no?however, when I run it, the first statement does not return an error like before, but there is nothing in Dec31, thus the second statement does not return an error, but does not find any files, because Dec31 is empty...
do you have any suggestion?
thanks a million...
Report Offensive Follow Up For Removal
Oh gosh - I am embarassed! Your discovery of .SS in the man pages is a new discovery for me also. So I guess I should go read the man page myself! :-D
The touch command you just posted is invalid on my system, and should be invalid on yours also. You need an extra 59 in it:
touch -t 2003123123.59 /tmp/Dec31 (bad)
touch -t 200312312359.59 /tmp/Dec31 (good)It means YYYYMMDDHHMI.SS
The touch command will change the datestamp on an existing file (if exists) or will create a new file. The new file will be empty as you have observed. That does not matter because we are interested in the date stamp of the file and not its contents.
Your touch command might be more lenient than mine since it did take your abbreviated time specification. Look at the date stamp to see what it created. If it simply assumed 00 minutes, then your date stamp would be Dec 31 23:00:59, which is not what we want but the find command should have still found some files.
At any rate, run the proper touch command, then the find command should work. And since we can in fact create a date stamp down to the nearest second, creating the date stamp of Dec 31, 2003 23:59:59 is exactly what you want.
And correct, the find command will locate all files having a modify date stamp more recent than the date stamp of the Dec31 file. And this would be date stamps of Jan 01, 2004 00:00:00 or later.
And let me know what date stamp your abbreviated time specification created.
I learned something today - thanks!
Report Offensive Follow Up For Removal
Hi Jim,
You are a great it works!
I had to try it with year 2002 because I needed to work on a test machine first.touch -t 200212312359.59 /tmp/Dec31
find $mydir/cups -type f -newer $workdir/Dec31 >file2.txtThe timestamp on Dec31 is:
14 Dec 31 2002 Dec31I got 14 files.
Thanks Jim.
camin
Report Offensive Follow Up For Removal
![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |