find command help

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
cconnell [ Mo, 14 Januar 2008 16:01 ] [ ID #1906989 ]

Re: find command help

On Mon, 14 Jan 2008 07:01:31 -0800 (PST), cconnell_1 [at] 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
Stephane CHAZELAS [ Mo, 14 Januar 2008 16:30 ] [ ID #1906992 ]

Re: find command help

On 2008-01-14, cconnell_1 [at] lycos.com <cconnell_1 [at] 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
Bill Marcum [ Mo, 14 Januar 2008 23:40 ] [ ID #1907013 ]

Re: find command help

On 14 Jan, 15:30, Stephane Chazelas <stephane_chaze... [at] yahoo.fr>
wrote:
> On Mon, 14 Jan 2008 07:01:31 -0800 (PST), cconnel... [at] 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
cconnell [ Di, 15 Januar 2008 00:08 ] [ ID #1907908 ]
Linux » comp.unix.shell » find command help

Vorheriges Thema: SED: replace all the underscores in the first word only.
Nächstes Thema: How to filter a .csv file based on the integer value in one specific field (per record)