0

I have thousands of backup files that are of the general format PICU_XX-YYYYYYYYYYY.Stp, where XX is a number and YYYYYYYYY is a Unix timedate stamp. Searching these files manually is difficult and erroneous when I use the windows file date and time. I want to be able to search for these files by using the unix datetime stamp generated.

Sample File Structure

./PICU_06-1328483198.Stp
./PICU_06-1328580000.Stp
./PICU_06-1329124034.Stp
./PICU_06-1329593129.Stp
./PICU_06-1329868449.Stp
./PICU_06-1329934425.Stp

I am trying to generate a small program that will take an input date/time, convert it to the Unix timedate system, and then find the files that are closest to the date/time.

Date example: 02/18/2012 17:25:29

Location example: 06

The date would convert to 1329607529. The next step would be to find the closest file before and after that time and date stamp. So it would take PICU_06 and find ./PICU_06-1329593129.Stp & ./PICU_06-1329868449.Stp

Start of my code:

echo Please enter LOCATION to search for. EX: 01 13 09A 22
read location
echo thanks
echo --------------------------------------------------

echo Please enter time to search for in MILITARY time.  Ex: MM/DD/YYYY HH:MM:SS
read startdatetime
echo thanks
echo --------------------------------------------------

date --date=\'$startdatetime\' +"%s"

As it stands, I can't get the datetime to work within the script to generate the second half of filename to search for.

Some of the errors i get are:

date: extra operand ‘+"%s"’

date: the argument ‘02:34:33'’ lacks a leading '+'; when using an option to specify date(s), any non-option argument must be a format string beginning with '+'

  • Possible duplicate of [How to convert strings like “19-FEB-12” to epoch date in UNIX](https://stackoverflow.com/questions/14805591/how-to-convert-strings-like-19-feb-12-to-epoch-date-in-unix) – TurtlesAllTheWayDown Nov 08 '17 at 18:02
  • Similar - but I want to script it such that I input a date/time. That seems to be where this is falling apart for me. – Tom Fogarty Nov 08 '17 at 18:23
  • 1
    You might want to try to change `--date=\'$startdatetime\'` to `--date="$startdatetime"` – etopylight Nov 09 '17 at 16:21

1 Answers1

0

Specify an input date format before output the timestamp:

date -jf '%d/%m/%Y %H:%M:%S' $startdatetime +"%s"
  • I get the following error: date: unknown option -- j – Tom Fogarty Nov 08 '17 at 18:29
  • I use cygwin64, if that matters. – Tom Fogarty Nov 08 '17 at 18:36
  • Alright, maybe the easiest thing to change is your input format. Indeed, according to the man, it seems that Cygwin follows the GNU date spec without that option. (http://www.polarhome.com/service/man/?qf=date&af=0&tf=2&of=Cygwin). Try to use '2004-02-29 16:21:42' format instead of your former one. – Jacques Bounliphone Nov 08 '17 at 18:54