LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Exclude directories when searching for files (https://www.linuxquestions.org/questions/linux-newbie-8/exclude-directories-when-searching-for-files-929258/)

Jakkie 02-14-2012 05:28 AM

Exclude directories when searching for files
 
Hi Guys,

When searching for a specific file in a directory, I use:

find /usr/test/files/ -iname "mytest*" -print0

This returns all "mytest" files from the specified directory. Unfortunatly, if a "mytest" file exists in a subdirectry, say "/usr/test/files/archive/", then this file is also returned. How can I exclude specific subdirectories from the search directory list? I basically only want to search in one directory, even if subdirectories exists. Also, when the files are returned, they are shown with the full path and file name. How do I get them shown with the name only?

Thanks!
J

acid_kewpie 02-14-2012 05:34 AM

you can use the -maxdepth value for find amongst various other approaches. This does largely make the use of find questionable. You probably don't want to use it, and just use ls instead or something? I'm presuming this is in a script, right? Maybe just a normal wildcard glob would suffice.

pinga123 02-14-2012 05:38 AM

Code:

find /usr/test/files/ -maxdepth 1 -iname "mytest*"
If you are quit sure that you only need to search in /usr/test/files/ and not in subdirectories you may opt for .
Code:

# # ls -ltr |grep -v "^d"|grep "mytest*"|awk '{print $9}'
this will show you only the filename .

Jakkie 02-14-2012 05:43 AM

Great, thanks guys. Its working. Any suggestion on the removal of the path before the file name? I just need the file name itself with no path included.

Thanks!
J

acid_kewpie 02-14-2012 06:26 AM

you can use basename to do that easily enough. alternatively if you were change directory and then run an ls IN that directory, you'd only have the short name to start with.

pinga123 02-14-2012 11:00 PM

Quote:

Originally Posted by Jakkie (Post 4601956)
Great, thanks guys. Its working. Any suggestion on the removal of the path before the file name? I just need the file name itself with no path included.

Thanks!
J

My second query does the same.

Jakkie 02-14-2012 11:02 PM

Yes, thank you Pinga123. Thanks for the help, I learn a lot!


All times are GMT -5. The time now is 03:45 PM.