[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bash conditional expressions

From: Greg Wooledge
Subject: Re: bash conditional expressions
Date: Thu, 18 Nov 2021 07:08:40 -0500

On Thu, Nov 18, 2021 at 07:26:51AM +0100, Michael J. Baars wrote:
> So now we have a relation for 'older than' and for 'newer than', but how 
> about 'oldest' (executable), and 'newest' (executable)?


The bot in libera's #bash also has this factoid for it:

Find the latest modified file in a directory: latest() { local file latest; for 
file in "${1:-.}"/*; do [[ $file -nt $latest ]] && latest=$file; done; printf 
'%s\n' "$latest"; } ## Usage: latest [dir]

> I could only come up with this:
> unset y; for x in $(find bin -mindepth 1 -name "*"); do if [[ ${x} -nt ${y} 
> ]]; then y=${x}; fi; done; echo newest: ${y};

That $(find) construct won't work for files with whitespace in their
names.  See <https://mywiki.wooledge.org/BashPitfalls#pf1>.  The -name "*"
bit is also kind of silly.

Apart from that, the basic premise (loop over all the files) is correct.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]