Unix Programming - find latest file

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2005 > find latest file





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author find latest file
pieter

2005-05-25, 6:02 pm

Anyone know how I can get unix to return only the latest file in a
directory with a given extension. I would like to rename that file
using a script

Consider I have a whole bunch of txt files. Let's say that a txt file
is generated through the execution of a make command. I would like to
write a second command (just below that and under the same target in my
makefile) to rename my txt file.

For those who are wondering why I don't just name the file what I want
outright, the problem is that the file that's generated is generated
with a numeric string over which I have no control. I'd like to rename
it, but I currently don't know how unless I do it manually.

I would think you could use the find command (something like below),
but I don't know how ... :

find -name *.txt ... -exec { mv ... }

joe@invalid.address

2005-05-25, 6:02 pm

"pieter" <pieter@gmail.com> writes:

> Anyone know how I can get unix to return only the latest file in a
> directory with a given extension. I would like to rename that file
> using a script
>
> Consider I have a whole bunch of txt files. Let's say that a txt
> file is generated through the execution of a make command. I would
> like to write a second command (just below that and under the same
> target in my makefile) to rename my txt file.
>
> For those who are wondering why I don't just name the file what I
> want outright, the problem is that the file that's generated is
> generated with a numeric string over which I have no control. I'd
> like to rename it, but I currently don't know how unless I do it
> manually.
>
> I would think you could use the find command (something like below),
> but I don't know how ... :
>
> find -name *.txt ... -exec { mv ... }


file=`ls -rt | tail -1`

Joe
Gordon Burditt

2005-05-25, 6:02 pm

>Anyone know how I can get unix to return only the latest file in a
>directory with a given extension. I would like to rename that file
>using a script
>
>Consider I have a whole bunch of txt files. Let's say that a txt file
>is generated through the execution of a make command. I would like to
>write a second command (just below that and under the same target in my
>makefile) to rename my txt file.


How about :

mv `ls -t *.txt | head -1` destination.txt

No guarantees if this directory contains subdirectories that
match *.txt.


>For those who are wondering why I don't just name the file what I want
>outright, the problem is that the file that's generated is generated
>with a numeric string over which I have no control. I'd like to rename
>it, but I currently don't know how unless I do it manually.


Your approach sounds very dangerous if you cannot be absolutely SURE
there will be only one make process (e.g. no manual editing) going on
in that directory.

Gordon L. Burditt
Floyd L. Davidson

2005-05-25, 6:02 pm

"pieter" <pieter@gmail.com> wrote:
>Anyone know how I can get unix to return only the latest file in a
>directory with a given extension. I would like to rename that file
>using a script
>
>Consider I have a whole bunch of txt files. Let's say that a txt file
>is generated through the execution of a make command. I would like to
>write a second command (just below that and under the same target in my
>makefile) to rename my txt file.
>
>For those who are wondering why I don't just name the file what I want
>outright, the problem is that the file that's generated is generated
>with a numeric string over which I have no control. I'd like to rename
>it, but I currently don't know how unless I do it manually.
>
>I would think you could use the find command (something like below),
>but I don't know how ... :
>
>find -name *.txt ... -exec { mv ... }


Have you considered reading the man page for the find command?

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com
Russell Shaw

2005-05-25, 6:02 pm

pieter wrote:
> Anyone know how I can get unix to return only the latest file in a
> directory with a given extension. I would like to rename that file
> using a script
>
> Consider I have a whole bunch of txt files. Let's say that a txt file
> is generated through the execution of a make command. I would like to
> write a second command (just below that and under the same target in my
> makefile) to rename my txt file.
>
> For those who are wondering why I don't just name the file what I want
> outright, the problem is that the file that's generated is generated
> with a numeric string over which I have no control. I'd like to rename
> it, but I currently don't know how unless I do it manually.
>
> I would think you could use the find command (something like below),
> but I don't know how ... :
>
> find -name *.txt ... -exec { mv ... }
>


For an extension of ".txt" :

ls -rt1 *.txt | tail -n1
Chuck Dillon

2005-05-25, 6:02 pm

pieter wrote:
> Anyone know how I can get unix to return only the latest file in a
> directory with a given extension. I would like to rename that file
> using a script
>
> Consider I have a whole bunch of txt files. Let's say that a txt file
> is generated through the execution of a make command. I would like to
> write a second command (just below that and under the same target in my
> makefile) to rename my txt file.


Yet another approach:

yourtarget:
touch .before
sleep 1
command-that-makes-the-file
find *.txt -newer .before -exec mv "{}" foo.txt \;

Testing left to the OP.

You might consider putting the above logic (or whatever you come up
with) in a script and have your makefile call the script so the naming
madness is confined to the script.

-- ced

>
> For those who are wondering why I don't just name the file what I want
> outright, the problem is that the file that's generated is generated
> with a numeric string over which I have no control. I'd like to rename
> it, but I currently don't know how unless I do it manually.
>
> I would think you could use the find command (something like below),
> but I don't know how ... :
>
> find -name *.txt ... -exec { mv ... }
>



--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com