0

I have a bash script which I need to convert from linux to FreeBSD, but I'm obviously too stupid to read the man page in a proper way.

The script has a line along:

date +%d -r "$file"

which works fine under linux, but just gives

date: illegal time format

on FreeBSD.

The linux man page says that -r is used to specify the referencre file:

   -r, --reference=FILE
          display the last modification time of FILE

What puzzles me here, is that the FreeBSD man page contains the same usage for this switch, but also provides an alternate one:

 -r seconds
     Print the date and time represented by seconds, where seconds is
     the number of seconds since the Epoch (00:00:00 UTC, January 1,
     1970; see time(3)), and can be specified in decimal, octal, or
     hex.

 -r filename
     Print the date and time of the last modification of filename.

Obviously the above error messages results from the expectation of a UNIX timestamp as parameter for the -r switch instead of the provided filename.

What I don't understand, is how I am supposed to make it clear that I want to use the second interpretation of the -r switch. If it is supposed to be deduced from the context of the call, I'm confused on how to provide this context.

Can anybody please explain to me, how I am supposed to tell the date utility, which use case of the -r switch I want to use here?

4
  • 1
    I searched for a question mark in your post, that might indicate you have an actual question you want answered. But there are only statements about FreeBSD and your level of understanding of things. This is a Q&A site, so please include a question in your post (if that is not appropriate you should consider posting this on some blog where you tell about your experiences with FreeBSD, date etc.)
    – Anthon
    Commented May 17, 2016 at 11:18
  • @Anthon I somehow thought the question was evident from my description of the problem ... none the less, I edited my question to make sure it contains an actual question mark - thank's for your feedback. ;-)
    – s1lv3r
    Commented May 17, 2016 at 11:24
  • try putting the +%d at the end.
    – meuh
    Commented May 17, 2016 at 11:26
  • @meuh Tried that already, unfortunately the error persists.
    – s1lv3r
    Commented May 17, 2016 at 11:27

2 Answers 2

2

Looks like this -r usage only made it into the sources on 7 May 2015. Perhaps your version doesn't do this yet?

Revision 282608 - (view) (download) (annotate) - [select for diffs] 
Modified Thu May 7 20:54:38 2015 UTC (12 months, 1 week ago) by delphij 

date(1): Make -r behave like GNU's version when the option can not be
interpreted as a number, which checks the file's modification time and
use that as the date/time value.
2

The script has a line along:

date +%d -r "$file"
which works fine under linux, but …

… on FreeBSD/PC-BSD will fail because the date command parses its command lines with getopt() and options must strictly precede arguments. The format string +%d is an argument, and must follow the -r option; otherwise -r isn't recognized as an option, but treated as an (invalid) argument. Note that the command synopsis on the date manual page displays them in this very order.

Yes, date treats the option arguments to -r as filenames only when it cannot decode them as numbers. Yes, this is a problem when one's filenames are numbers. ☺

A better approach for reading the last modification time of a file/directory, without worrying about whether its name happened to resemble a number or not, would be the stat command … had that not significant difficulties of its own.

case "`uname`" in
Linux) stat -c '%y' -- "$1"|cut -c9-10 ;;
*BSD) stat -f '%Sm' -t '%d' -- "$1" ;;
esac

2
  • On that system date +%d -r /etc/passwd itself (from shell) also gives the error. So meuh must be right for my case, but thank you very much for providing stat as an alternative. :-)
    – s1lv3r
    Commented May 17, 2016 at 15:08
  • For me on a Mac, using this suggestion made it work. Perhaps consider editing the answer to provide the working case up top of date -r "$file" +%d
    – Traveler
    Commented Apr 29, 2018 at 20:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .