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.

8
  • 1
    You want the date directly as it is displayed in ls -l? Commented Jun 25, 2014 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. Commented Jun 25, 2014 at 22:26
  • Oh, so we're having an XY problem it seems… Commented Jun 25, 2014 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. Commented Jun 25, 2014 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… Commented Jun 25, 2014 at 22:46

2 Answers 2

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) 
Sign up to request clarification or add additional context in comments.

7 Comments

It doesn't seem like my date command supports the -r option; all it seems to support is -u and -a.
@steph what system are you using?
I'm using putty on Windows to connect to a Linux server.
@steph but what linux server is it? what distribution? what version?
@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.
|
0

Use stat with the correct format option:

stat -c%y file 

2 Comments

I seem to get an error when I run the stat command; specifically, I get "stat: Command not found."
It's located in /bin/stat on my machine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.