LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to search a combination of directory and file? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-search-a-combination-of-directory-and-file-4175716591/)

bsmile 09-09-2022 10:26 AM

how to search a combination of directory and file?
 
For example, I want to search a pattern like directory/file, how to do that?

If I simply use

Code:

find . -name directory/file
then the system would complain

find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name 'BASIC_REF/WAVECAR'' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ 'foo/file''.

astrogeek 09-09-2022 10:37 AM

The answer is in the error message you posted...

Code:

You might find the '-wholename' test more useful, or perhaps '-samefile'.
Use the -wholename or -iwholename option! That was new to me but worked first try, see man find!

For example:

Code:

find ./path -iwholename '*subdir/filename'
Thanks - I learned something new before lunch today!

wpeckham 09-09-2022 10:38 AM

1. Have you considered combining find with grep to accomplish the search?
2. You have been doing this stuff for a while. Had you not encountered the "locate" command?
3. What distribution and version are you using? The default tools (and versions) differ so it is always good to mention this in your question post.

bsmile 09-09-2022 01:26 PM

Quote:

Originally Posted by astrogeek (Post 6379162)
The answer is in the error message you posted...

Code:

You might find the '-wholename' test more useful, or perhaps '-samefile'.
Use the -wholename or -iwholename option! That was new to me but worked first try, see man find!

For example:

Code:

find ./path -iwholename '*subdir/filename'
Thanks - I learned something new before lunch today!

Glad you learned something, but too bad I still couldn't get it. What is returned from the following two examples are

Code:

find .  -iwholename 'filename'
does not return anything [let's assume filename is some file in my system]

Code:

find ./path -iwholename '*subdir/filename'
find: './path': No such file or directory

Anything further help?

bsmile 09-09-2022 01:36 PM

Quote:

Originally Posted by wpeckham (Post 6379163)
1. Have you considered combining find with grep to accomplish the search?
2. You have been doing this stuff for a while. Had you not encountered the "locate" command?
3. What distribution and version are you using? The default tools (and versions) differ so it is always good to mention this in your question post.

Thanks, I successfully use find + grep to achieve my goal.

I am a light linux user and search/ask when I ran into a specific issue. So far I only encountered find, and never used it with confidence (constantly failed to return what I want). I have not had a chance to use "locate" yet. I am looking for solutions which can be generically applied to most linux versions, thus hadn't tried to provide my linux version, which is actually linux mint mate64 installed in virtualbox.

astrogeek 09-09-2022 02:18 PM

Quote:

Originally Posted by bsmile (Post 6379183)
Glad you learned something, but too bad I still couldn't get it. What is returned from the following two examples are

Code:

find .  -iwholename 'filename'
does not return anything [let's assume filename is some file in my system]

Code:

find ./path -wholename '*subdir/filename'
find: './path': No such file or directory

Anything further help?

First, -wholename must match the pattern, so filename must match exactly and it will not match if in a subdirectory. You must write the pattern to match exactly what you are looking for including any intervening subdirectory names (a leading '*' will match any leading path names).

Next, './path' in my example is not literal, it should be the path on your system down which you want to search. For example to search from the current directory use '.' or './', or to search your home directory use '~/'.

Using your original example, the find command might be like this (tested locally):
Code:

ls -l snippets/another/BASIC_REF/
total 0
-rw-r--r-- 1 user user 0 Sep  9 13:03 WAVECAR

find . -wholename '*BASIC_REF/WAVECAR'
./snippets/another/BASIC_REF/WAVECAR

The wildcard '*' is necessary to match within any sub-directory along the search path, for hopefully obvious reasons.

Hope this helps!


All times are GMT -5. The time now is 12:30 AM.