• Post Reply Bookmark Topic Watch Topic
  • New Topic

Find command  RSS feed

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody ,
Here is a big problem with find command

I want find x.txt file from current directory
so for this i am using find . -name "x.txt"
so, it gives exactly like this ./x.txt

but i need the result to be given exactly as x.txt
is there nay way to do so?
Please I am trying from morning but still i couldn't
there are some commands like grep,cut,awk commands but all these files require file input
i need this command to run and display only the file name not any other "./ " caracters preceding the file name
Please help me out .. sooon
I 'll be waiting for your reply messages

Thanks you very much in advance!!
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please anybody help me out
here i got one more solution while trying dont knwo whether it is right way or not ...
find . -name "x.txt">Temp.txt | cut -d '/' -f 2 Temp.txt
but here there is some flaw...
if the file is present in sub directory of dir1
then it displays dir1 instead of displaying x.txt
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one way:

find . -name logging.properties -printf "%P\n"

And if you only want the filename itself:

find . -name logging.properties -printf "%f\n"

I've added they "\n" to the end of the output to make it more readable on the console.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Holloway:
Here's one way:

find . -name logging.properties -printf "%P\n"

And if you only want the filename itself:

find . -name logging.properties -printf "%f\n"

I've added they "\n" to the end of the output to make it more readable on the console.


No Tim its not working for KSH shell scripting ...

Here is some other way i could get it now..

name=$(find . -name "x.*" | cut -f $3 -d '/')
echo $name

This is working fine but more specific
so now i need to make it generic
here in this cut -f $3 -d '/'
i want to remove 3
for this i need to find the last occurance of '/'

so one more simple question in ksh
how could i get count of occurences of '/' in a given string ?
please can you provide a unix command for this?
 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this

cut the name of the file from 3 character, as shown below

find . -name x.txt | cut -c 3-

this works in ksh also
[ January 17, 2008: Message edited by: minal silimkar ]
 
Doug Slattery
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saikrishna,

but here there is some flaw...
if the file is present in sub directory of dir1


Try the basename command in the pipeline. If that gives you the leading './', then use cut to fix up your output.

Hope this helps...

Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea why this wouldn't work with ksh, since it's a single program being executed with no shell-specific features.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Holloway:
I have no idea why this wouldn't work with ksh, since it's a single program being executed with no shell-specific features.


-printf isn't a POSIX option. I just checked and the 'find' on my MacOSX machine doesn't have it, but on my FC7 machine, it's there.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need for pipelining:
 
Doug Slattery
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need for pipelining:

code:


find ./ -name "a.txt" -exec basename {} \;



True, however for each file that is found, the -exec flag will spawn a child process anyway. One for each occurrence. In a pipeline, each occurrence can be handled in a for loop, so there is only one child process spawned. More efficient. For loops in the shell don't themselves spawn processes. Really though, the find would be the child process in this case. In a pipeline, commands to the right of the '|' character are parents to the ones on the left .

Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

doesn't work for me, because basename just handles one file per call - unlike ls for example.
So I need one basename-process per file anyway - don't I?

My manpage suggests to use the + this way:

but that gives me an error.

What's about this:
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is the right command, without \; while using +
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!