I am trying to get a time stamp and convert it to the format - YYYYMMDD
. I don't know how to get the date without using stat and I can't install stat because it is a company server. I have looked around but it seems everyone would use stat.
Asked
Active
Viewed 2,709 times
0

chopper
- 6,649
- 7
- 36
- 53

user2438390
- 3
- 1
- 6
-
"I have a bolt I need loosened, but I lost my wrench and vice grips." `stat` or a program that uses the `stat()` call are pretty much your options. Did you try the output from `ls -l` or similar and parse it yourself? – Dark Falcon Mar 03 '14 at 21:57
-
1I would try `find test.file -printf "%t\n"` - however I guess that this will also need `stat` in the back. That's why I don't post it as an answer. If `find` works, you can change the format as well. Just `man find` and search for `printf`. – Karsten S. Mar 03 '14 at 22:02
-
@KarstenS [SunOS find](http://www.manpages.info/sunos/find.1.html) supports `-printf`. This is a good solution. – that other guy Mar 03 '14 at 22:14
-
getting result find: bad option -printf – user2438390 Mar 03 '14 at 23:12
3 Answers
2
If you can't use GNU stat
, maybe you can sneak under the radar and use Perl's stat :-)
perl -MPOSIX -e 'print POSIX::strftime "%Y%m%d\n", localtime((stat $ARGV[0])[9])' yourfile
Result:
20140303
Many thanks to Glenn Jackman (see Comments below) for magnificently filling the role of "clever person" and showing me where I was going wrong. Thank you.

Mark Setchell
- 191,897
- 31
- 273
- 432
-
-
1You need to convert the epoch time from stat into the array required by strftime: `perl -MPOSIX -e 'print POSIX::strftime "%Y%m%d", localtime((stat $ARGV[0])[9])'` – glenn jackman Mar 04 '14 at 00:49
-
1@jaypal :-) You make him sound like an oracle - or dragon, maybe ;-) – Mark Setchell Mar 04 '14 at 09:13
0
ls -l --time-style="+%Y%m%d" filename

Amit
- 19,780
- 6
- 46
- 54
-
getting an illegal option. I might just need parse the ls -l filename return seperate – user2438390 Mar 03 '14 at 22:56
0
Expanding from Amit's answer to just return the timestamp
ls -l --time-style="+%Y%m%d" ./filename |cut -d " " -f 6

rgunning
- 568
- 2
- 16