Sample Header Ad - 728x90

Most robust way to list every basename in a directory, sorted by modification date?

7 votes
3 answers
960 views
Given a directory containing: * note 1.txt, last modified yesterday * note 2.txt, last modified the day before yesterday * note 3.txt, last modified today What is the best way to fetch the array note 3 note 1 note 2? To define "best," I'm more concerned about robustness (in the context of Zsh in macOS) than I am about efficiency and portability. The intended use case is a directory of hundreds or thousands of plain text files, but—at the risk of muddling the question—this is a specific case of a more general question I have, of what best practices are in performing string manipulations on filepaths printed by commands like ls, find, and mdfind. --- I've been using a macro which invokes this command to achieve the above:
ls -t | sed -e 's/.[^.]*$//'
It's never failed, but: * [Greg's Wiki](http://mywiki.wooledge.org/EnglishFrontPage) strongly recommends against parsing the output of ls. ([Parsing ls](http://mywiki.wooledge.org/ParsingLs) ; [Practices](http://mywiki.wooledge.org/BashGuide/Practices) , under "5. Don't Ever Do These"). * Is invoking sed inefficient where parameter expansion would do? Using find (safely delimiting filepaths with NUL characters rather than newlines), and parameter expansion to extract the basenames, this produces an unsorted list:
find . -type f -print0 | while IFS= read -d '' -r l ; do print "${${l%.*}##*/}" ; done
But sorting by modification date would seem to require invoking stat and sort, because macOS's find lacks the -printf flag which [might otherwise serve well](https://superuser.com/a/546900) . Finally, using Zsh's [glob qualifiers](http://zsh.sourceforge.net/Doc/Release/Expansion.html#Glob-Qualifiers) :
for f in *(om) ; do print "${f%.*}" ; done
Though not portable, this last method seems most robust and efficient to me. Is this correct, and is there any reason I shouldn't use a modified version of the find command above when I'm actually performing a search rather than simply listing files in a directory?
Asked by seanakabry (73 rep)
Jul 25, 2020, 08:23 AM
Last activity: Jan 19, 2023, 04:17 AM