LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-09-2022, 10:26 AM   #1
bsmile
Member
 
Registered: Oct 2017
Posts: 98

Rep: Reputation: Disabled
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''.

Last edited by bsmile; 09-09-2022 at 01:42 PM.
 
Old 09-09-2022, 10:37 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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!

Last edited by astrogeek; 09-09-2022 at 11:09 AM. Reason: cleaned up my own mess!
 
Old 09-09-2022, 10:38 AM   #3
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,587

Rep: Reputation: 2687Reputation: 2687Reputation: 2687Reputation: 2687Reputation: 2687Reputation: 2687Reputation: 2687Reputation: 2687Reputation: 2687Reputation: 2687Reputation: 2687
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.
 
Old 09-09-2022, 01:26 PM   #4
bsmile
Member
 
Registered: Oct 2017
Posts: 98

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
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?

Last edited by bsmile; 09-09-2022 at 01:48 PM.
 
Old 09-09-2022, 01:36 PM   #5
bsmile
Member
 
Registered: Oct 2017
Posts: 98

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by wpeckham View 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.
 
Old 09-09-2022, 02:18 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by bsmile View Post
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Does a command, or a combination of commands exist that would move a bunch of files into a new directory and once that directory is full ... wh33t Linux - Software 12 08-04-2022 07:55 PM
[SOLVED] Search and replace backslash variable combination using sed works with CLI but not script petersfreeman Linux - Desktop 2 10-01-2021 02:42 PM
cat command - for every combination of 6 files, merge into a single image file. Numptius Linux - Newbie 13 06-28-2018 02:40 AM
how to modify binary file by using combination command (dd, hexdump, sed, ...)??? bvdyi Linux - Newbie 2 11-07-2013 01:28 AM
Bind Mouse Button Combination to Keyboard Key Combination? neoAKiRAz Linux - Desktop 0 05-04-2007 12:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration