Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Closed Thread
  Search this Thread
Old 08-03-2010, 05:26 AM   #1
Registered: May 2009
Posts: 181
Blog Entries: 9

Rep: Reputation: 15
By way of thanks for all the help.

A script to generate random dates. It uses the year range 2006-2009, and truncates every month of the year to an ordinary February's 28 days, but otherwise it's pretty solid and safe.

while [ $spate -le 120 ]
NUMBER=$[ ( $RANDOM % 864000 ) + 1 ]
SHTMON=$[ ($RANDOM%27) + 1 ]
if [ ${#SHTMON} = 1 ]; then day=$(echo "0"$SHTMON); else day=$SHTMON; fi
if [ ${#ERNUMB} = 1 ]; then mon=$(echo "0"$ERNUMB); else mon=$ERNUMB; fi
if [ ${#RNUMBER} = 1 ]; then year=$(echo "0"$RNUMBER); else year=$RNUMBER; fi
b=$[NUMBER / 60]
c=$[b * 60]
d=$[b / 60]
e=$[d * 60]

#Padding single-length time entries with zeros
if [ ${#seconds} = 1 ]; then sec=$(echo "0"$seconds); else sec=$seconds; fi
if [ ${#minutes} = 1 ]; then min=$(echo "0"$minutes); else min=$minutes; fi
if [ ${#hours} = 1 ]; then hrs=$(echo "0"$hours); else hrs=$hours; fi
longtime=$(echo $hrs$min.$sec) #<< the random time in 'touch -t'-friendly format (seconds marked off by a period, not a colon).
deatt=$(echo $year$mon$day) #<< Setting date in 'touch -t' format
time=$(echo $deatt$longtime) #<<Putting the date and time together, keeping the formats (no spaces)
echo "$time">>randomtime.txt
echo "Written - random date number $spate."
YMMV, but when I used the following script on a folder of 120 JPEG files, no two dates were the same. The times? I'm not positive they were likewise all unique, but at a glance I didn't see much repetition.
while read 'line';
nardo=$(echo $line)
chachi=$(echo $nardo | cut -d, -f2)
joanie=$(echo $nardo | cut -d, -f1)
richie=$(basename "$joanie")
echo "Working on file $richie now."
touch -t $chachi $joanie

And to think this was going to be the topic of a question, me looking for help yet again! Four hours of Google-ing, cross-referencing online MAN and Info pages, and coming across near-to-topic and spot-on threads in forums such as LQ and Unix-Linux Forums (, and such as that gave me just enough where I could turn it around and make this offering.

Again, thanks.


Last edited by SilversleevesX; 08-03-2010 at 05:31 AM.
Old 08-03-2010, 07:34 AM   #2
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

Closed Thread

bash, date, file, random, time, touch

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:23 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration