0

Basically, I can see that

ls -l

displays the date that all the files in a directory were last edited. What I want to do is to access the date and then store it into a variable using a .sh script. However, not exactly sure what I can do to do so, and I've looked up the man pages for ls as well as searched up the matter to no avail.

Tremaine
  • 89
  • 2
  • 9
  • 1
    You want the date directly as it is displayed in `ls -l`? – merlin2011 Jun 25 '14 at 22:19
  • It could be any format, actually. I'm just trying to compare the dates of each file against each other, so as long as they are all in the same format, it'd be fine. – Tremaine Jun 25 '14 at 22:26
  • Oh, so we're having an [XY problem](http://mywiki.wooledge.org/XyProblem) it seems… – gniourf_gniourf Jun 25 '14 at 22:31
  • Well, when you put it that way, then that's probably the case. I should have more clearly defined my question first. – Tremaine Jun 25 '14 at 22:35
  • You know, you can edit your question and change it to something like: _how can I find all files that have the same ctime as a given file?_ or something similar (if that's what you're trying to do). Also, please specify you linux distribution and version… – gniourf_gniourf Jun 25 '14 at 22:46
  • Did you missed this post which have the answer already http://stackoverflow.com/questions/11212663/filename-last-modification-date-shell-in-script – Rahul Jun 25 '14 at 22:57
  • @steph I made a [mostly working implementation of `ls`](http://stackoverflow.com/a/22839267/3076724) at some point. Should probably help you get the output you're looking for. – Reinstate Monica Please Jun 25 '14 at 23:02
  • If your still havving problems -- Have you checked my updated answer – Tasos Jun 25 '14 at 23:05

2 Answers2

1

If your date command supports the -r option, you can use it:

variable=$(date -r file)

The advantage of the date command is that you'll be able to format the date in many ways. For example, in seconds since Epoch:

variable=$(date -r file +%s)
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
  • It doesn't seem like my date command supports the -r option; all it seems to support is -u and -a. – Tremaine Jun 25 '14 at 22:28
  • @steph what system are you using? – gniourf_gniourf Jun 25 '14 at 22:29
  • I'm using putty on Windows to connect to a Linux server. – Tremaine Jun 25 '14 at 22:31
  • 1
    @steph but what linux server is it? what distribution? what version? – gniourf_gniourf Jun 25 '14 at 22:33
  • @steph, How come you don't have `-r` option? this is kind a best solution (to my knowledge). At least a +1 from me. Even I came up with same. – Rahul Jun 25 '14 at 22:53
  • @BroSlow, May be but then that should be tagged accordingly instead we do some speculation. Asking for help without any effort from OP's side. That's just great. – Rahul Jun 25 '14 at 23:10
  • The speculation is also about what the OP really wants—I'd speculate OP wants to determine files that have same ctime, or something close to that, and in any case nothing related to `ls`/`date`/`stat` whatsoever. – gniourf_gniourf Jun 25 '14 at 23:13
0

Use stat with the correct format option:

stat -c%y file
choroba
  • 231,213
  • 25
  • 204
  • 289