how to search a combination of directory and file?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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''.
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.
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.
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!
Last edited by astrogeek; 09-09-2022 at 02:25 PM.
Reason: potys
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.