LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How To grep only directory (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-grep-only-directory-942384/)

rabir 04-29-2012 06:27 AM

How To grep only directory
 
Hi Everyone.

i am newbie at Linux n trying to find out a file in which directory. as
find / -name abir* but
results shows as
/root/Desktop/abir/abir.sh
/root/Desktop/abir/abir.txt

but i need only directory as
/root/Desktop/abir/

Thanks in Advance for Help.

catkin 04-29-2012 06:28 AM

Use find's -d option.

rabir 04-29-2012 06:33 AM

not working find's -d

rabir 04-29-2012 06:36 AM

i am using Red hat Enterprise Linux 5.4

Thanks 4 replay

catkin 04-29-2012 06:38 AM

Sorry, should be -type d

rabir 04-29-2012 06:42 AM

so the full command should be
find / -name -type d abir* or
find / -type d abir*

Thanks 4 replay

catkin 04-29-2012 06:50 AM

Code:

find / -type d -name 'abir*'
The single quotes avoid abir* being changed by the shell file name expansion

rabir 04-29-2012 06:57 AM

still not working
i try it as
find / -type d -name 'abir*' &
find / -type d -name "abir*" &
find / -type d -name abir* &&
find / -name abir* -type d


thanks 4 replay

rabir 04-29-2012 07:06 AM

is there any option that extract only directory as

v1=/root/Desktop/abir/abir.txt

from that variable i can take only the part of

v2=/root/Desktop/abir/

thanks 4 help

catkin 04-29-2012 07:19 AM

If directory /root/Desktop/abir exists and is reachable by the user (does ls -d /root/Desktop/abir work?) then find / -type d -name 'abir*' should work.

If it does not work, is there any output?

What is the output of type find ?

rabir 04-29-2012 07:33 AM

i usually work as root. so no problem to reach any directory.

i also make some practice on your command as
if any name in the directory as "abir" then it works as

/root/Desktop/abir

but
if the directory is /root/Desktop/rabir/abir.txt

then your command is not working .

but i need only the directory that the file stay in ?

thanks for response

grail 04-29-2012 08:18 AM

I think you are saying that you wish to search for files containing abir but return the directory they reside in. Is this correct?

Assuming yes, try this:
Code:

find / -type f -name 'abir*' -printf "%h\n" | uniq
I put the uniq in as your examples show that there might be more than one abir file in a directory and every occurrence will return the directory name.
Take it off if this is not the case.

rabir 04-29-2012 10:31 AM

it works.

Thanks

grail 04-29-2012 11:32 AM

No problem. Please mark as SOLVED once you have a solution.


All times are GMT -5. The time now is 11:34 PM.