find command help

find command help

am 14.01.2008 16:01:31 von cconnell

hello
I need some help with the find command, i need the find command to
return the 'most recent file' called ftp_events* in /var/log but not
descending the directory. If there is no file in there I just want the
output to be blank.
By recent I mean the last modified as from ls -tr | tail -1.

I have tried

find /var/log -maxdepth 1 -name "FTPevents*" -exec ls -tr {} \; |
sort | tail -1

but the above doesnt seem to work very well

when I use xargs, it has funny behaviour in that if the file isnt
there - it runs ls on the current directory!

find /var/log -name "FTPevents" | xargs ls

i thought xargs only applied commands to the files found?

Thanks for any help

Re: find command help

am 14.01.2008 16:30:52 von Stephane CHAZELAS

On Mon, 14 Jan 2008 07:01:31 -0800 (PST), cconnell_1@lycos.com wrote:
> hello
> I need some help with the find command, i need the find command to
> return the 'most recent file' called ftp_events* in /var/log but not
> descending the directory. If there is no file in there I just want the
> output to be blank.
> By recent I mean the last modified as from ls -tr | tail -1.
[...]

find is not the most appropriate tool here.

With zsh, that would simply be /var/log/ftp_events*(om[1])

If your shell is not zsh, why not

ls -td /var/log/ftp_events* | head -1

> when I use xargs, it has funny behaviour in that if the file isnt
> there - it runs ls on the current directory!
>
> find /var/log -name "FTPevents" | xargs ls
>
> i thought xargs only applied commands to the files found?
[...]

and if there's no file found, xargs runs the command with no
argument. The GNU implementation of xargs has the -r option to
avoid that.

An answer your question with GNU tools:

find /var/log -maxdepth 1 -name 'ftp_events*' -printf '%TS\t%p\0' |
sort -rzn |
tr '\0\n' '\n\0' |
head -1 |
cut -f2- |
tr '\0' '\n'

--
Stephane

Re: find command help

am 14.01.2008 23:40:45 von Bill Marcum

On 2008-01-14, cconnell_1@lycos.com wrote:
>
>
> hello
> I need some help with the find command, i need the find command to
> return the 'most recent file' called ftp_events* in /var/log but not
> descending the directory. If there is no file in there I just want the
> output to be blank.
> By recent I mean the last modified as from ls -tr | tail -1.
>
> I have tried
>
> find /var/log -maxdepth 1 -name "FTPevents*" -exec ls -tr {} \; |
> sort | tail -1

Do you even need find for this? Why not just
ls -tr /var/log/FTPevents* | tail -1

Re: find command help

am 15.01.2008 00:08:18 von cconnell

On 14 Jan, 15:30, Stephane Chazelas
wrote:
> On Mon, 14 Jan 2008 07:01:31 -0800 (PST), cconnel...@lycos.com wrote:
> > hello
> > I need somehelpwith thefindcommand, i need thefindcommandto
> > return the 'most recent file' called ftp_events* in /var/log but not
> > descending the directory. If there is no file in there I just want the
> > output to be blank.
> > By recent I mean the last modified as from ls -tr | tail -1.
>
> [...]
>
> findis not the most appropriate tool here.
>
> With zsh, that would simply be /var/log/ftp_events*(om[1])
>
> If your shell is not zsh, why not
>
> ls -td /var/log/ftp_events* | head -1
>
> > when I use xargs, it has funny behaviour in that if the file isnt
> > there - it runs ls on the current directory!
>
> >find/var/log -name "FTPevents" | xargs ls
>
> > i thought xargs only applied commands to the files found?
>
> [...]
>
> and if there's no file found, xargs runs thecommandwith no
> argument. The GNU implementation of xargs has the -r option to
> avoid that.
>
> An answer your question with GNU tools:
>
> find/var/log -maxdepth 1 -name 'ftp_events*' -printf '%TS\t%p\0' |
> =A0 sort -rzn |
> =A0 tr '\0\n' '\n\0' |
> =A0 head -1 |
> =A0 cut -f2- |
> =A0 tr '\0' '\n'
>
> --
> Stephane

Hello thanku very much, i wanted to use find, because I didnt want an
error if the dir was empty and i was using the contents of the string
to copy the file i wanted

so the xargs -r option worked well:

find /var/log -maxdepth 1 -type f -name "FTPevents*" | xargs -r ls -tr
| tail -1