0

I'm trying to find a simple command in bash that opens the last added file in a directory. I came across this and a few other questions but they were always about the last modified, in contrast with last added. Is there a simple way to do this?

Community
  • 1
  • 1
hsnee
  • 543
  • 2
  • 6
  • 17
  • Depending on your filesystem the command `stat ` may give you a `Birth:` date of the file. Run `stat yourfile.txt` and see if that field is filled out. Otherwise, linux doesn't track file creation dates, only modified, so you may be out of luck. – JNevill Mar 29 '16 at 19:19
  • What's the real goal here? Presumably to use the latest file for some other process? Do the file names contain the date at all? Do they have some other usable component that can be sorted/etc. on? – Etan Reisner Mar 29 '16 at 19:23
  • I'm using terminal on a Mac. Running `stat` returns a bunch of values including what I believe is "modified" "created" and "added". I'd be looking for the largest "added" value. – hsnee Mar 29 '16 at 19:25

1 Answers1

4

I think this answers why the thing you need is not possible.

Linux filesystems either just don't record creation time or if they do there is no way to access it through standard command line tools. However, there are ways to access it as described here for example.

For systems that do record the time, such as Mac OS X as mentioned in the other question, you can adapt the code you found by using:

ls -tU | head -n1
Community
  • 1
  • 1
Marcus H
  • 56
  • 4