开发者

List newest file, by type (.txt), after searching recursively, in a terminal

I'm trying to get my terminal to return the latest .txt file, with pat开发者_JAVA百科h intact. I've been researching ls, grep, find, and tail, using the '|' functionality of passing results from one utility to the next. The end result would be to have a working path + result that I could pass my text editor.

I've been getting close with tests like this: find . | grep '.txt$' | tail -1

..but I haven't had luck with grep returning the newest file - is there a flag I'm missing?

Trying to use find & ls isn't exactly working either:

find . -name "*.txt" | ls -lrth

..the ls returns the current directories instead of the results of my find query.

Please help!


You're so very close.

vi "$(find . -name '*.txt' -exec ls -t {} + | head -1)"


find /usr/share -name '*.txt' -printf '%C+ %p\n' | sort -r | head -1 | sed 's/^[^ ]* //'


If you have bash4+

ls -t ./**/*.txt | head -1

edit the latest txt file

vim $(ls -t ./**/*.txt |head -1)

ps: need enabled shopt -s globstar in your .bashrc or .profile...


You can use the stat function to print each file with just the latest modification time and name.

find . -name "*.txt" -exec stat -c "%m %N" {} \; | sort

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜