How do I get the name of the newest file via the Terminal?

Posted by Alec on Stack Overflow See other posts from Stack Overflow or by Alec
Published on 2010-05-15T00:58:33Z Indexed on 2010/05/15 1:04 UTC
Read the original article Hit count: 347

Filed under:
|

I'm trying to create a macro for Keyboard Maestro for OS X doing the following:

  1. Get name of newest file in a directory on my disk based on date created;
  2. Paste the text "newest file: " plus the name of the newest file.

One of its options is to "Execute a shell script", so I thought that would do it for 1. After Googling around a bit I came up with this:

cd /path/to/directory/
ls -t | head -n1

This sorts it right, and returns the first filename. However, it also seems to includes a line break, which I do not want. As for 2: I can output the text "newest file: " with a different action in the app, and paste the filename behind that. But I'm wondering if you can't return "random text" + the outcome of the ls command.

So my question is: can I do this only using the ls command? And how do I get just the name of the latest file without any linebreaks or returns?

© Stack Overflow or respective owner

Related posts about shell

Related posts about command-line