[Top][All Lists]

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

Re: copy

From: Greg Wooledge
Subject: Re: copy
Date: Tue, 20 Jul 2021 18:25:15 -0400

On Tue, Jul 20, 2021 at 09:38:20PM +0000, Val Krem wrote:
> In one folder I  have list of  several  files. Based on a condition (grep) I 
> want pick the latest  file from all files  and and copy this file  to 
> somehwere.

Latest by modification time?

> Here is my  sample  of the zipped files and my attempt
> Sample  files
>     AB1_2002.csv
>     AB1_2003.csv
>     AB1_2008.csv
>     AB2_2005.csv
> I used this,
>  ll | grep -e  AB1 |tail -1
> the result is
>     AB1_2008.csv

So, you simply want the last one in sorting order?  No need to look at
the modification times?

That's easy enough.  Store the filenames in an array and take the last

files=( *AB1* )         # Or maybe you want AB1*.csv

cp -- "$lastfile" /wherever/you/want/

Do not use ls (or ll which I presume is an alias).  Do not use grep.  Do
not use tail.  All of those tools assume various things about filenames
which are not true in all cases.  In particular, they assume that filenames
can't contain newline characters -- but they can.  At least on Unix.

Using the array handles all filenames no matter how bizarre they are,
and as a bonus, it'll be more efficient, because you aren't running
a bunch of external programs.

reply via email to

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