LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-17-2016, 08:42 AM   #1
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Rep: Reputation: Disabled
Exclamation Can find be restricted to the current directory


Hello Friends,

The find searches recursively the required files.
But can it be made to search only the given path and not the sub-directories ?
For example, i want to search only in /home/san. I don't want to look up in the sub-directories under san.
Is there any workaround ?
 
Old 06-17-2016, 08:44 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,250

Rep: Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423
Read the man page

Code:
man find

       -maxdepth levels
              Descend at most levels (a non-negative integer) levels of directories below the command line arguments.  -maxdepth 0
               means only apply the tests and actions to the command line arguments.
 
1 members found this post helpful.
Old 06-17-2016, 11:08 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by techie_san778 View Post
Is there any workaround ?
"-maxdepth"? is not a "workaround".
Code:
find /home/san -maxdepth 1 -name <file>
Have a Great Day!
 
1 members found this post helpful.
Old 06-17-2016, 12:04 PM   #4
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
Well guys, maxdepth is not POSIX compliant. I used the prune option but of no use :
$ find /home/san -name c++ -prune -ls
How should prune be used then ?
 
Old 06-17-2016, 12:27 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by techie_san778 View Post
Well guys, maxdepth is not POSIX compliant.
You really could save everyone their valuable time by posting your "requirements" up front.
http://catb.org/~esr/faqs/smart-questions.html
5 years now...
 
Old 06-17-2016, 01:37 PM   #6
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
You really could save everyone their valuable time by posting your "requirements" up front.
I have no intention to spoil anybody's time here, so called "busy" boy.
The fact is that i came to know that "maxdepth" is not POSIX compliant after i had posted here. And what was your reply ?
Quote:
Originally Posted by Habitual View Post
"-maxdepth"? is not a "workaround".
So what is the workaround ? Are you not spoiling time of others ?
 
1 members found this post helpful.
Old 06-17-2016, 01:59 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by techie_san778 View Post
I have no intention to spoil anybody's time here, so called "busy" boy.
The fact is that i came to know that "maxdepth" is not POSIX compliant after i had posted here.
Our time is valuable.
Our experience invaluable.
The proper answer starts with "Thank you."

End Transmission.

Last edited by Habitual; 06-17-2016 at 02:08 PM.
 
Old 06-17-2016, 04:28 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
Quote:
Originally Posted by techie_san778 View Post
Well guys, maxdepth is not POSIX compliant. I used the prune option but of no use :
$ find /home/san -name c++ -prune -ls
How should prune be used then ?
Why not just: ls /home/san/c++
 
Old 06-18-2016, 02:24 PM   #9
BCarey
Senior Member
 
Registered: Oct 2005
Location: New Mexico
Distribution: Slackware
Posts: 1,639

Rep: Reputation: Disabled
Quote:
Originally Posted by techie_san778 View Post
Well guys, maxdepth is not POSIX compliant. I used the prune option but of no use :
$ find /home/san -name c++ -prune -ls
How should prune be used then ?
The position of prune is important because it applies to the preceding argument. Try:

find /home/san/* -prune -name blabla

Note the asterisk because otherwise -prune will also exclude the /home/san directory itself.

Brian
 
Old 06-20-2016, 07:02 AM   #10
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
Thanks Brian about that info about prune. But when i use it to list all the hidden files and directories under a directory (/home/san) :
[san@Desktop ~]$ find /home/san/* -prune -name ".*"
[san@Desktop ~]$

I am getting blank o/p. Please rectify me where i am going wrong.

Added : But when i am using find without prune, i am able to list all the hidden files and folders, though find behaves recursively :
[san@Desktop ~]$ find /home/san -name ".*"
/home/san/.local
/home/san/test1/.output
/home/san/.esd_auth
/home/san/.ed_cmdhist_script
/home/san/.gnome2_private
/home/san/.vimrc
/home/san/.thumbnails
/home/san/.viminfo
/home/san/.redhat
--------------------- // List is big. Truncating rest.

Sandy

Last edited by techie_san778; 06-20-2016 at 07:05 AM.
 
Old 06-20-2016, 07:23 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 17,555

Rep: Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930
What are you looking for? Do you need only the list of entries, or something else too?
I would suggest you to use for example python or perl or similar to open a dir and get list of entries. That will be even faster than the command find. You can also try ls | awk or similar construct.
 
Old 06-20-2016, 08:06 AM   #12
pingu_penguin
Member
 
Registered: Aug 2004
Distribution: Manjaro Linux
Posts: 315

Rep: Reputation: 57
Hi,

Try -maxdepth 1 without the prune option but give it first , and the options later.

something like this :

# find /path/to/dir -maxdepth 1 -type f -name '*.c' -print

etc adding other options after maxdepth for filtering what you want.

Hope this helps.
 
Old 06-20-2016, 08:20 AM   #13
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
What are you looking for? Do you need only the list of entries, or something else too?
I wanted to know that whether find command can be made to search non-recursively, i.e.
within a directory only, not inside it's sub-directories.
That having said, i came to know that the "maxdepth" is not POSIX compliant. So, i was trying out the "prune" option, but i am unable to find the hidden files & directories.
Can you suggest a solution ?
 
Old 06-20-2016, 08:45 AM   #14
pingu_penguin
Member
 
Registered: Aug 2004
Distribution: Manjaro Linux
Posts: 315

Rep: Reputation: 57
did you try this ?

# find /path/to/dir -maxdepth 1 -name '.*'

I don't know what POSIX compliance has to do with it but , yes maxdepth 1 is used to find non-recursively, and it works too.

Additionally you could use " -type d -o -type f " if there are other types of files in there too (like socket , block , or symbolic link files) to filter to your needs.
 
Old 06-20-2016, 09:05 AM   #15
techie_san778
Member
 
Registered: Jul 2011
Posts: 90

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pingu_penguin View Post
did you try this ?
# find /path/to/dir -maxdepth 1 -name '.*'
Yes Pingu, it does the job. Thanks for the solution mate.
 
  


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
Find current Thunar directory from CLI? jsteel Linux - General 3 04-21-2011 03:05 AM
[SOLVED] How to find files under the current directory? thomas2004ch Linux - Newbie 2 10-05-2010 06:19 AM
find lists current directory kaprasanna Linux - General 3 12-17-2007 05:06 AM
find in current directory ramanadyv Linux - Newbie 3 03-28-2006 11:12 PM
how do I find out the current working directory? nodger Programming 2 12-26-2004 12:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:08 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