Bash programming, copy only onefile?

Giorgos Keramidas keramida at ceid.upatras.gr
Tue Aug 24 17:56:07 PDT 2004


On 2004-08-24 17:15, Nathan Kinkade <nkinkade at ub.edu.bz> wrote:
> On Wed, Aug 25, 2004 at 12:16:17AM +0200, Joachim Dagerot wrote:
> > A quiz easy to write, hard to answer?
> >
> > In bash, how can I write a command that moves the oldest file in a
> > directory to a new direction?
>
> Here is one possible way, certainly there are many others:
>
> # ls -t /path/to/dir | tail -n 1 | xargs -i{} cp {} /path/to/location

In FreeBSD 5.X there's also stat(1) which can print the modification
time of files in a numeric format and can be used in pipes like this:

    % stat -f '%m %N' * | sort -n | head -1 | cut -d ' ' -f 2-

This should print the filename of the file with the oldest modification
time.  Access time or creation time can also be shown using the -f 'fmt'
argument of stat(1) but details about that can be found in the stat(1)
manpage.

- Giorgos



More information about the freebsd-questions mailing list