LinuxQuestions.org
Visit Jeremy's Blog.
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-09-2016, 10:42 AM   #1
Ned Radd
LQ Newbie
 
Registered: Apr 2016
Posts: 20

Rep: Reputation: Disabled
How to use "/" as part of Find command?


When I mount partitions, they go under /media/... path. There are other folders I want to exclude from searches as well, such as /mnt /proc /dev and so on. I know that to search from / I have to enter that as \/. I also have to use multiple -name <folder> -purge -o arguments to exclude each folder. But to explicitly exclude /media I need to include the "/", and find won't let me do this. Passing just "media" is not what I want to do, as then media could be any other folder or file with that same exact name. I want the exclusion to be this specific folder at that level and no other. How can I make this happen?
 
Old 06-09-2016, 12:38 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,416
Blog Entries: 13

Rep: Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522
How about you post some specific find command sequences which you are talking about and the qualifications which you wish to filter for exclude. I've not done exclude much, I'll pipe to grep or something. Here are a few that I do, and yes they are just simple ones:
Code:
$ find . -name "*.c" -exec grep MyApi {} /dev/null \;
$ find ./devdir/v1.0/src "*.[ch]" -exec grep MyApi_ {} /dev/null \;
So by using these, I either start from where I am, or I start at a particular directory top level, I only find the file types I wish. Here they would be source files or source and reference include files, and then I exec a grep for a particular string within all the found files. I use the /dev/null to get rid of a lot of the error outputs, however I could also use a -s switch for the grep, but this is just how I've always done something like this.

I fully understand that you wish to exclude and once or twice years ago I did use that. Perhaps you can show what you're trying and where it's not serving you.

To add to all this, I do not believe there is a specific exclude option for the find command.

What you need to do is give it a directory list, which you can, and also from there you can specify -maxdepth to control how many levels down each directory tree it will go. That becomes difficult two ways (1) you can select -maxdepth 1 and then have a very long list of path names, or (2) you can select -maxdepth <higher number> and then you may have to still deal with going too far for your preferences for some subs.
 
Old 06-09-2016, 12:53 PM   #3
robertdaleweir
Member
 
Registered: Jul 2006
Location: Canada
Distribution: Fedora
Posts: 93

Rep: Reputation: 11
Quote:
Originally Posted by Ned Radd View Post
When I mount partitions, they go under /media/... path. There are other folders I want to exclude from searches as well, such as /mnt /proc /dev and so on. I know that to search from / I have to enter that as \/. I also have to use multiple -name <folder> -purge -o arguments to exclude each folder. But to explicitly exclude /media I need to include the "/", and find won't let me do this. Passing just "media" is not what I want to do, as then media could be any other folder or file with that same exact name. I want the exclusion to be this specific folder at that level and no other. How can I make this happen?
Hi Ned
You cannot exclude '/' as that is the 'top of the entire directory tree'. Hence you are saying exclude the entire 'system tree' which means no search can take place at all. You should however be able to exclude '/media'. I would say that use './media' but i do not think '/' has a parent, and hence this may not work. Can you provide the exact command that you are trying so we might examine it more precisely.
Code:
find / -path /media -prune -o -name '*.txt' -print

Last edited by robertdaleweir; 06-09-2016 at 01:01 PM.
 
Old 06-09-2016, 06:14 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,859

Rep: Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827
Code:
find / \( -path /media -o -path /proc -o -path /mnt \) -prune -o -print
You must use an explicit print, or the -prune would implicitly print.
 
Old 06-10-2016, 12:39 PM   #5
Ned Radd
LQ Newbie
 
Registered: Apr 2016
Posts: 20

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by robertdaleweir View Post
Hi Ned
You cannot exclude '/' as that is the 'top of the entire directory tree'. ... Can you provide the exact command that you are trying so we might examine it more precisely.
Code:
find / -path /media -prune -o -name '*.txt' -print
First, I wasn't trying to exclude root, I was pointing out that find was rejecting any path that began with '/'. Second, your command won't work unles you are root or super user, so it must begin with 'sudo ' for most users, and they need admin powers or be otherwise members of the sudo group for that to work. So it is more correctly stated as
Code:
sudo find / -path /media -prune -o -name '*.txt' -print
Next, the "-print" argument is essentially superfluous as the output will go to stdout anyway. Then there is the matter of using '/' alone. Awhile back, that was not /' possible, and I found I had to use '\/' together to search from the root. Apparently that has been fixed as your version worked perfectly.

And that is why I thank you for your answer. You brought in '-path', an argument I was not familiar with. Yes, it's there, but 'find' is so versatile, so flexible, and the man, info, and --help details so extensive that it is easy to get loss in them. With -path, '/media' is accepted, which was not the case without it. The error message from find was not clear in that regard.
 
Old 06-10-2016, 01:36 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,579

Rep: Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097
Quote:
Originally Posted by Ned Radd View Post
Next, the "-print" argument is essentially superfluous as the output will go to stdout anyway.
It is not superfluous. In the absence of an action term like "-print", a default "-print" is assumed for every term of the expression, and that includes the term "-path /media -prune". So, without that "-print" bound to the "-name '*.txt'" term, you will see "/media" (just the directory name -- nothing from within it) in the output along with ".txt" files. With that explicit "-print", there is no default action taken, and "/media" does not appear in the output.
 
Old 06-10-2016, 01:38 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
Quote:
Originally Posted by Ned Radd View Post
First, I wasn't trying to exclude root, I was pointing out that find was rejecting any path that began with '/'. Second, your command won't work unles you are root or super user, so it must begin with 'sudo ' for most users, and they need admin powers or be otherwise members of the sudo group for that to work. So it is more correctly stated as
Code:
sudo find / -path /media -prune -o -name '*.txt' -print
Next, the "-print" argument is essentially superfluous as the output will go to stdout anyway. Then there is the matter of using '/' alone. Awhile back, that was not /' possible, and I found I had to use '\/' together to search from the root. Apparently that has been fixed as your version worked perfectly.
Wrong. It has worked quite well for any user. You just are not allowed to search directories that you are forbidden to access.

It has been that way ever since it has existed...
Quote:

And that is why I thank you for your answer. You brought in '-path', an argument I was not familiar with. Yes, it's there, but 'find' is so versatile, so flexible, and the man, info, and --help details so extensive that it is easy to get loss in them. With -path, '/media' is accepted, which was not the case without it. The error message from find was not clear in that regard.
If you just want to search the /media directory tree it is simpler to do "find /media ...". Of course, there is still the requirement that you have permission.
 
  


Reply

Tags
arguments, exclude, find, folders, specific


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 illegal characters in Windows via "find" command and replace them automatically. hack3rcon Linux - Software 5 02-29-2016 08:52 AM
[SOLVED] "net rpc" "failed to connect to ipc$ share on" or "unable to find a suitable server" larieu Linux - General 0 11-09-2014 01:45 AM
Using a single "Find" Command to find files bases on multiple criteria roboxooo Linux - Newbie 6 01-15-2009 05:13 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM

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

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