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

前端 未结 6 844
渐次进展
渐次进展 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:48

    Since this comes up first when I search for "find newest file on mac shell" then I thought I would post for others to help...

    If you are trying to find the file in just that directory ls is good, but I found that find works quite well, and you might learn something new in the process. I however had to get the GNU find on my mac in order to use the line of code that diimdeep posted above.

    If you run Homebrew, which you should if you spend any time in terminal, or maybe one of the alternatives. You can run "brew install findutils" Once this is finished the code from above will work like this...

    sudo /usr/local/opt/findutils/bin/gfind -type f -printf '%T+ %p\n' | sort -r | head -n 1
    

    Just change the last bit to a 10 if you want more. gfine will take a path if you need to specify that as well. I usually cd into the folder that I want to start in though. Find is automatically recursive.

提交回复
热议问题