Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have no idea why the ordinary Web surfer thinks Google is superior to the other search engines. (Or has that changed?) I am attaching a screenshot of hits that came up for me in response to the above search string.
In the meantime, maybe I can pick your brains for an answer or two to the implied question: "How do I generate a set of random dates (and times) and re-format them for use with GNU touch (the -t option; all numbers and one dot delimiting seconds)?"
In short:
Code:
YYMMDDhhmm.ss
I have found a few good scripts to do the Year-Month-Day (pseudo)random generation in a specified range, but I'm looking for a way to generate random times (in a 24-hour range).
you can use $RANDOM to generate a random number, $RANDOM % 24 to generate a random number between 0 and 23 (you can use as hour). Similar way you can generate 60 minutes and 60 seconds:
I have no idea why the ordinary Web surfer thinks Google is superior to the other search engines. (Or has that changed?)
Off-topic 'cause you already have a couple of excellent responses that should solve your question:
I prefer duckduckgo for searching. No tracking, no ads.
...but then, I'm probably not an "ordinary Web surfer"
You didn't provide any specifics with regards to allowable ranges...
I was looking to generate dates with ranges (in years) from 1999 to 2013, in times, all 24 hours in a day.
I think pan64's method may be better for this than the one you (suicidaleggroll) suggested. I'm inclined to try that one first, and if it fails, to try yours. Breaking the output down to GNU touch -t-ready strings should be no problem, as I already have a nice routine from my gimmetdate script, to wit:
where "dart" is the date-time string in standard stat -c %y output format.
As it happens, I just found a file on the Net where the server-side mod date has a year of 2017, which I definitely want to change. So I'll know right away which random date-time method garners the best results.
Carver
Last edited by L_Carver; 06-28-2017 at 10:29 AM.
Reason: Added punctuation.
Off-topic 'cause you already have a couple of excellent responses that should solve your question:
I prefer duckduckgo for searching. No tracking, no ads.
...but then, I'm probably not an "ordinary Web surfer"
Thank you for the suggestion. My LM 18 install had duckduckgo for the default search engine in Firefox and, when I found and installed them, one or two other browsers. I'm thinking now I should have stuck with it.
I was looking to generate dates with ranges (in years) from 1999 to 2013, in times, all 24 hours in a day.
I think pan64's method may be better for this than the one you (suicidaleggroll) suggested.
Um. Both of them suggested the same solution, to wit: Use $RANDOM
Quote:
Originally Posted by L_Carver
Breaking the output down to GNU touch -t-ready strings should be no problem, as I already have a nice routine from my gimmetdate script, to wit:
date: invalid date ‘@17897989834.74600000000000000000’
message. (the above is just one example.) Is there any way to stop this from happening? Or is it part of the nature of RANDOM?
Your example works fine with mine, maybe you have an older version of date that is limited in its valid range? Once you scale it down to fit within the range posted by schneidz I imagine that will stop happening.
$ date --version
date (GNU coreutils) 8.26
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David MacKenzie.
Code:
$ date -d @17897989834.74600000000000000000
Fri Mar 1 16:50:34 CET 2537
And I sort of understand how to set the ranges/parameters, but I need help combining
Code:
date -d 1999-01-01 +%s
et cetera with RANDOM.
Well you wouldn't combine that command with $RANDOM. The -d flag in date says to use the date you provide rather than the current date. You need to feed $RANDOM into -d, not a specific date like you've shown here. The +%s flag in date tells it what output format to use. In this case, %s means "seconds since 1970-01-01 00 UT", or what is more traditionally known as the Unix Epoch.
So "date -d 1999-01-01 +%s" specifically means, tell me what Jan 1, 1999 was in Unix Epoch time.
Anyway, for your problem schneidz provided you with the valid epoch time ranges, 915166800 to 1388466000. That's a range of 473299200. $RANDOM covers 0-32767, if you square it and multiply by 0.4408 that'll get you basically there, then just add 915166800. You might miss out on a couple of hours on the top end of that time range, but you can adjust the precision as neceesary.
Should I look to install a newer version? Is there an i386 deb
(Enough on that -- got the coreutils-8.26-3ubuntu3_i386.deb and installed it with gdebi. It wasn't available on the pages for Xenial [16.04] on packages.ubuntu.com, but I found it on the pages for Zesty [16.10]. Time will tell if the bug fix I presume is in the better/newer version of date pans out.)
..generate random dates & times in my preferred range.
Indeed, the upgrade of coreutils did "cure" that behaviour with 'date.'
Code:
#!/bin/bash -i
echo "Generating a random modified date (for use with 'touch -t')"
temp=$(echo "$RANDOM * $RANDOM * 0.4408 + 915166800" | bc -l)
tdate=$(date -d @$temp +%C%y%m%d%H%M.%S)
echo "Your new modified date is $tdate ."
/bin/echo -n "$tdate" | xsel -b
echo "It is now on your clipboard."
exit 0
I've changed my mind about the range of dates I want to generate with the script. Is there any way to specify the "highest" year to generate dates in/for? Having dates like
Code:
202005201744.32
seems a little bizarre to me. I'm much more inclined to make 2016 my maximum year in the range, and set 2012 for the start of the range.
Looking forward to some useful answers, per usual.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.