3

Say I have several files that are generated by a script and follow the pattern:

yyyymmdd_fileName.fileExtension

And yyyymmdd (i.e. 20170202) is a date, where year is 2017, month is February and day is the 2nd).

I know I can get the complete file name and extract the date part, but how do I make it a date element so I can compare with the current date provided by date?

3

Use -d arg of date.

man date

   -d, --date=STRING
          display time described by STRING, not 'now'
$ date -d 20170202
Thu Feb  2 00:00:00 CET 2017

To ease the comparsion, you can use the unixdate format

$ date -d 20170203 +%s
1486076400
| improve this answer | |
  • -d is specific to GNU date; on BSD date it sets the kernel's value for daylight saving time. – John N Feb 2 '17 at 10:28
0

Another option is to use touch. This should work on most systems, but you need to specify time as hhmm.ss. I normally use 1200.00.

touch -t 201702021200.00
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.