Project

General

Profile

[CLI] BASH: One-liner (long) for changing stat dates to Touch "-t" dates.

Added by Steve Wright over 8 years ago

One of the annoying things, for me, about using the 'touch' command to change file dates is that, with the '-d' option, you always have to use quotation marks. I suppose it's an annoyance because I'm frequently forgetting to close the quotes, and so I get errors. I prefer the -t option. But stat doesn't 'give it to you. So I came up with this line to convert one to the other, syntax-wise.

(FYI: You're getting this before the Linux forums do.)

I'm sure if folks gave it some thought, they'd have come up with this on their own, but here I've saved you the trial and error.

Works in BASH 4.1.x in Cygwin and Ubuntu 11.04 (not tested in other environments).

tdate=(stat -c %y yourfile.jpg);
breakdate=${tdate%-*} ; onedate=$(echo "$breakdate" | tr -d ":" | tr -d "-" | tr -d " ") ; ctdate=$(echo $onedate | sed 's/^\(.\{12\}\)/\1./'); tdate=$ctdate

You can thank me later. G

SJW


Replies (1)

RE: [CLI] BASH: One-liner (long) for changing stat dates to Touch "-t" dates. - Added by Robin Mills over 8 years ago

Thanks for posting this, Steve. It's a while since we've heard from you. Hope you're well and doing good.

Robin

    (1-1/1)