problems with FIND cmd

problems with FIND cmd

Post by DJ » Sat, 01 May 2004 04:29:08



How do I restrict the output of the find command to just the file
name?

Here is the code:

thefile=$(find .  -newer trace_file_tmp -print)
 if [ -n "$thefile" ] ; then
       do something
 fi

Here is the directory it is operating in:  

-rwxr-xr-x   1 oracle     dba   611 Apr  9 11:16
ora_19842_elmcig76.trc
-rw-r--r--   1 oracle     dba   0 Apr 29 14:18   test
-rw-r--r--   1 oracle     dba   0 Apr 28 14:12   trace_file_tmp

With this code I am getting $thefile shown in debug mode:

thefile=.
./test
+ [ -n .
./test ]

With $thefile set to . or ./test it is not working like I need it.
How do I restrict find to only "test"?
Any suggestion will be appreciated.

thanks,

DJE

 
 
 

problems with FIND cmd

Post by Norm Dresne » Sat, 01 May 2004 06:33:52



Quote:> How do I restrict the output of the find command to just the file
> name?

> Here is the code:

> thefile=$(find .  -newer trace_file_tmp -print)
>  if [ -n "$thefile" ] ; then
>        do something
>  fi

> Here is the directory it is operating in:

> -rwxr-xr-x   1 oracle     dba   611 Apr  9 11:16
> ora_19842_elmcig76.trc
> -rw-r--r--   1 oracle     dba   0 Apr 29 14:18   test
> -rw-r--r--   1 oracle     dba   0 Apr 28 14:12   trace_file_tmp

> With this code I am getting $thefile shown in debug mode:

> thefile=.
> ./test
> + [ -n .
> ./test ]

> With $thefile set to . or ./test it is not working like I need it.
> How do I restrict find to only "test"?
> Any suggestion will be appreciated.

> thanks,

1.  Assuming that the file you want to find is always called "test", then
adding
        -name   test
    to the find command would find it
2.  Assuming that the problem is that it's also finding the file used in
the -newer test as well, you could add a pipe to grep to exclude it.
        find ....     |   grep  -v trace_file-tmp
    If the file name has to be variable, make it an argument to a script or
function

        Norm

 
 
 

problems with FIND cmd

Post by Charles Dem » Sat, 01 May 2004 06:52:01




>How do I restrict the output of the find command to just the file
>name?

pipe find's output through basename?

Chuck Demas

Quote:>Here is the code:

>thefile=$(find .  -newer trace_file_tmp -print)
> if [ -n "$thefile" ] ; then
>       do something
> fi

>Here is the directory it is operating in:  

>-rwxr-xr-x   1 oracle     dba   611 Apr  9 11:16
>ora_19842_elmcig76.trc
>-rw-r--r--   1 oracle     dba   0 Apr 29 14:18   test
>-rw-r--r--   1 oracle     dba   0 Apr 28 14:12   trace_file_tmp

>With this code I am getting $thefile shown in debug mode:

>thefile=.
>./test
>+ [ -n .
>./test ]

>With $thefile set to . or ./test it is not working like I need it.
>How do I restrict find to only "test"?
>Any suggestion will be appreciated.

>thanks,

>DJE

--
  Eat Healthy        |   _ _   | Nothing would be done at all,

  Die Anyway         |    v    | That no one could find fault with it.

 
 
 

problems with FIND cmd

Post by Chris F.A. Johnso » Sat, 01 May 2004 07:22:30





>>How do I restrict the output of the find command to just the file
>>name?

> pipe find's output through basename?

     Basename does not read stdin:

$ thefile=$(find .  -newer trace_file_tmp -print | basename)
basename: too few arguments
Try `basename --help' for more information

Quote:>>Here is the code:

>>thefile=$(find .  -newer trace_file_tmp -print)
>> if [ -n "$thefile" ] ; then
>>       do something
>> fi

>>Here is the directory it is operating in:  

>>-rwxr-xr-x   1 oracle     dba   611 Apr  9 11:16 ora_19842_elmcig76.trc
>>-rw-r--r--   1 oracle     dba   0 Apr 29 14:18   test
>>-rw-r--r--   1 oracle     dba   0 Apr 28 14:12   trace_file_tmp

>>With this code I am getting $thefile shown in debug mode:

>>thefile=.
>>./test
>>+ [ -n .
>>./test ]

>>With $thefile set to . or ./test it is not working like I need it.

    What is not working? If you get an error, post it; if you get
    unexpected or undesired results, post the actual results as well
    as what you want.

Quote:>>How do I restrict find to only "test"?

    It's not clear what you want. Do you just want the one file? Or do
    you want the leading ./ stripped off? Or both?

    If you just want the one file, and it will always be called test,
    use that instead of .:

find test -newer trace_file_tmp -print

    If you want to strip the leading ./, and have the GNU version of
    find:

find -newer trace_file_tmp -printf "%f\n"

    Some versions of test (the command, not your file) have the -nt
    operator:

[ test -nt trace_file_tmp ] && echo test

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

problems with FIND cmd

Post by Charles Dem » Sat, 01 May 2004 07:42:45







>>>How do I restrict the output of the find command to just the file
>>>name?

>> pipe find's output through basename?

>     Basename does not read stdin:

ok, pipe it through:

sed 's:.*/::g'

Chuck Demas

--
  Eat Healthy        |   _ _   | Nothing would be done at all,

  Die Anyway         |    v    | That no one could find fault with it.

 
 
 

problems with FIND cmd

Post by Barry Margoli » Sat, 01 May 2004 09:31:32






> >How do I restrict the output of the find command to just the file
> >name?

> pipe find's output through basename?

I don't think you understood the question.  The problem isn't the
directory portion of the filename, the problem is that the output
contains two names, "." and "./test".

I suspect the solution is to use -type f in the find options, to keep it
from showing directories.

Quote:

> Chuck Demas

> >Here is the code:

> >thefile=$(find .  -newer trace_file_tmp -print)
> > if [ -n "$thefile" ] ; then
> >       do something
> > fi

> >Here is the directory it is operating in:  

> >-rwxr-xr-x   1 oracle     dba   611 Apr  9 11:16
> >ora_19842_elmcig76.trc
> >-rw-r--r--   1 oracle     dba   0 Apr 29 14:18   test
> >-rw-r--r--   1 oracle     dba   0 Apr 28 14:12   trace_file_tmp

> >With this code I am getting $thefile shown in debug mode:

> >thefile=.
> >./test
> >+ [ -n .
> >./test ]

> >With $thefile set to . or ./test it is not working like I need it.
> >How do I restrict find to only "test"?
> >Any suggestion will be appreciated.

> >thanks,

> >DJE

--

Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
 
 
 

problems with FIND cmd

Post by rakesh shar » Sat, 01 May 2004 12:58:15



> How do I restrict the output of the find command to just the file
> name?

Simple, insert the '-type f' clause in your 'find' statement.e.g.,

    find . -type f -newer trace_file_tmp -print

Quote:

> With $thefile set to . or ./test it is not working like I need it.
> How do I restrict find to only "test"?

tfile=test
if [ -n "`find $tfile -newer trace_file`" ]; then
  # ... do something
fi
 
 
 

problems with FIND cmd

Post by DJ » Sun, 02 May 2004 00:22:52


Thanks for everybodys help.  I did this and it worked:

 thefile=$(find .  -type f  -newer trace_file_tmp -print)
    basename $thefile
    if [ -n "$thefile" ] ; then
       head -100 $thefile > ${OUTPUTFILE}
       echo  "New trace file $thefile  in ${UDUMPDIR}"  |
/usr/sbin/sendmail email  < ${OUTPUTFILE}
    fi

Use the -type f,  it was picking up a directory (i.e. .) And the
basename on the variable.  Since I am here:

1. How do I use a subject with sendmail?

2. How do I get a linefeed (i.e. \n ) in the echo to sendmail.  I have
put \n in the echo and tired printf and nothing has worked.

 
 
 

problems with FIND cmd

Post by Kevin Colli » Sun, 02 May 2004 03:28:04



> Thanks for everybodys help.  I did this and it worked:

>  thefile=$(find .  -type f  -newer trace_file_tmp -print)
>     basename $thefile

      ^^^^^^^^^^^^^^^^^^

This will only print the base name of the file, it does not change the value of
$thefile - is that what you want?

Quote:>     if [ -n "$thefile" ] ; then head -100 $thefile > ${OUTPUTFILE} echo  "New
>     trace file $thefile  in ${UDUMPDIR}"  | /usr/sbin/sendmail email  <
>     ${OUTPUTFILE} fi

> Use the -type f,  it was picking up a directory (i.e. .) And the basename on
> the variable.  Since I am here:

> 1. How do I use a subject with sendmail?

Try the mail or mailx command (depending on your platform) with the '-s
subject' option, unless you really have to use sendmail - it is much simpler.

If you need to use sendmail, you can add header entries before the 1st blank
line. For example:

 /usr/sbin/sendmail email  <<EOF Subject: here is my subject Cc:

$(cat ${OUTPUTFILE}) EOF

Quote:> 2. How do I get a linefeed (i.e. \n ) in the echo to sendmail.  I have put \n
> in the echo and tired printf and nothing has worked.

I'm not quite sure I understand, but I suspect you mean you want additional
linefeeds in the received email. If that is the case, I will assume you are
using MS Outlook and that it is "auto-formattingyour messages. If that is also
true, turn off auto-formatting in Outlook.

Kevin

 
 
 

1. problem with find cmd

I am having a problem with the find cmd when trying to find files that
were modified more than n days ago. For example,
todays date : 2000/11/30

The cmd I use is :
find /tmp -type f -name 'rp*.log' -mtime +6 -ls 2>/dev/null

But note that the file created on 20001121 ( look at file name ) and
not touched since Nov 23,  does not appear on the list !!

-rw-r--r--   1 smatta   staff    2760704 Nov 23 23:34
rpServer.20001121.114112.log

Any ideas ?
TIA,

Sunil
(Also : has anyone noticed that the find cmd that actually matches
files will print the name on the files,  even with the explicit -print
option set ! this never used to happen before ! )

Sent via Deja.com http://www.deja.com/
Before you buy.

2. Read/Write permissions for VFAT mounts

3. find cmd: full pathnames problem

4. commercial X servers, opinions please

5. Sort of OT - problem with "find" cmd inside ksh script

6. Is there notebook for SCO UNIX

7. VAR=$(cmd) versus VAR=`cmd`

8. Problem with Linux Netscape

9. capturing exit status for cmd when invoking 'xterm -e cmd'

10. how to assign out=`cmd` w/out waiting for "cmd" to finish??

11. ksh/bash: let cmd and test cmd interpret 08 (octal) differently

12. ksh/bash expr `cmd` vs $(cmd)

13. ksh/bash: let cmd and test cmd interpret 08 (octal) differently