[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
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
Latest by modification time?
> Here is my sample of the zipped files and my attempt
> Sample files
> I used this,
> ll | grep -e AB1 |tail -1
> the result is
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.
- copy, Val Krem, 2021/07/20
- Re: copy,
Greg Wooledge <=