I'm trying to create a macro for Keyboard Maestro for OS X doing the following:
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?
Since you're already using pipes, just throw another one in there:
ls -t | head -n1 |awk '{printf("newest file: %s",$0)}'
(Note that the "printf" does not include a '\n' at the end; that gets rid of the linebreak)
Edit:
With Arkku's suggestion to exit awk after the first line, it looks like:
ls -t | awk '{printf("newest file: %s",$0);exit}'
cd /path/to/directory/
echo -n "random text goes here" $(ls -t | head -n1)
If you want, you can add more text on the end in the same way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With