LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-14-2012, 05:28 AM   #1
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Rep: Reputation: Disabled
Smile 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
 
Old 02-14-2012, 05:34 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

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

Last edited by acid_kewpie; 02-14-2012 at 05:35 AM.
 
Old 02-14-2012, 05:38 AM   #3
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

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

Last edited by pinga123; 02-14-2012 at 05:46 AM.
 
Old 02-14-2012, 05:43 AM   #4
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-14-2012, 06:26 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 02-14-2012, 11:00 PM   #6
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by Jakkie View Post
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.
 
Old 02-14-2012, 11:02 PM   #7
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
Yes, thank you Pinga123. Thanks for the help, I learn a lot!
 
  


Reply



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
Searching Directories for files containing blah linuxbird Linux - Desktop 1 07-16-2010 11:11 AM
tar exclude directories kais1 Linux - Newbie 1 10-13-2009 09:01 AM
CVS Exclude : Exclude sub directories from check out On Linux from command line shajay12 Linux - Newbie 1 08-03-2009 12:36 AM
How to exclude directories in tar? depam Linux - General 3 05-07-2007 08:23 PM
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 12:23 PM

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

All times are GMT -5. The time now is 06:42 PM.

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