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

前端 未结 6 854
渐次进展
渐次进展 2021-01-12 12:29

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;
6条回答
  •  天命终不由人
    2021-01-12 12:50

    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}'
    

提交回复
热议问题