LinuxQuestions.org
Visit Jeremy's Blog.
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 10-23-2017, 11:25 AM   #1
peter7089
Member
 
Registered: May 2016
Distribution: MX Linux
Posts: 163

Rep: Reputation: Disabled
'find' command - list all paths containing the search string?


If i do a search like this 'locate init.d' it lists all paths containing 'init.d':
Code:
/etc/init.d
/etc/init.d/anacron
/etc/init.d/bluetooth
/etc/init.d/console-setup.sh
/etc/init.d/cron
/etc/init.d/dbus
/etc/init.d/hwclock.sh
/etc/init.d/keyboard-setup.sh
/etc/init.d/kmod
/etc/init.d/networking
/etc/init.d/procps
/etc/init.d/rsyslog
/etc/init.d/ssh
/etc/init.d/sudo
/etc/init.d/udev
How can i do this with the 'find' command? I tried 'sudo find / -name *init.d*' but it didn't work, or at least i get a different result:

Code:
/sys/module/parport_pc/sections/.init.data
/sys/module/libata/sections/.init.data
/sys/module/scsi_mod/sections/.init.data
/etc/init.d
 
Old 10-23-2017, 11:27 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,940

Rep: Reputation: 780Reputation: 780Reputation: 780Reputation: 780Reputation: 780Reputation: 780Reputation: 780
see the man page, hint -type d
 
Old 10-23-2017, 12:23 PM   #3
peter7089
Member
 
Registered: May 2016
Distribution: MX Linux
Posts: 163

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Keith Hedger View Post
see the man page, hint -type d
Not working:
Code:
max@debian:~$ sudo find / -type d -name  *init.d*
/etc/init.d
 
Old 10-23-2017, 12:40 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
Try with quotes so that the shell does not process the asterisks before sending the results to find.

Code:
$ sudo find / -type d -name "*init.d*" -print
See also "man find" versus "man 7 glob"
 
Old 10-23-2017, 01:15 PM   #5
peter7089
Member
 
Registered: May 2016
Distribution: MX Linux
Posts: 163

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Code:
$ sudo find / -type d -name "*init.d*" -print
See also "man find" versus "man 7 glob"
I get the same output:
Code:
max@debian:~$ sudo find / -type d -name "*init.d*" -print
/etc/init.d
 
Old 10-23-2017, 01:48 PM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
Sorry. I should have read the original example more closely.

Code:
find / -path '*init.d*' -print
Be sure to check the other options in the manual page

Code:
man find
Also, remember that between the options there is an implied logical AND. If you want a logical OR, you have to pay attention to operator precedence and often group options using parenthesis.
 
Old 10-24-2017, 01:50 AM   #7
peter7089
Member
 
Registered: May 2016
Distribution: MX Linux
Posts: 163

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post

Code:
find / -path '*init.d*' -print
It kind of works but also lists paths from /proc directory with 'permission denied' at the end of the line even so i am running the command as root. I guess it's better to stick with 'locate' command for that kind of searches.
 
Old 10-24-2017, 02:03 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
You can exclude things by using the -prune option

Code:
find / \( -path '/proc' -o -path '/run' -o -path '/sys' -o -path '/tmp' \) -prune \
        -o -path '*init.d*' -print
There will be nothing relevant in the /proc directory. See "man 7 hier" about that.

What are you really trying to do?
 
Old 10-24-2017, 05:10 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,505

Rep: Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113
In addition to what Turbocapitalist posted locate only searches a database versus the actual filesystem. The database stores paths/filenames as defined in the /etc/updatedb.conf file and by default typically excludes everything that is not part of the "base" system i.e. no external media, network shares, virtual filesystems i.e /proc etc. It is also typically only updated once a day so that it can contain stale or incomplete data if searching for something that was recently created or deleted.

find searches the actual directory tree and so includes everything in your case since your starting from the top. It has many options and can be quite complicated so what search tool to use depends on what you want to find. As you have concluded if all you want is to find file names based upon a pattern locate is much simpler.
 
Old 10-24-2017, 01:35 PM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,263
Blog Entries: 10

Rep: Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160
Quote:
Originally Posted by peter7089 View Post
It kind of works but also lists paths from /proc directory with 'permission denied' at the end of the line even so i am running the command as root. I guess it's better to stick with 'locate' command for that kind of searches.
use find with '2>/dev/null' appended.
it's also good to remember that locate and find do two fundamentally different things.
 
Old 10-24-2017, 01:50 PM   #11
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 753

Rep: Reputation: 327Reputation: 327Reputation: 327Reputation: 327
I like the -xdev (== -mount) switch, as long as I keep in mind what filesystems I have, from df
There's also a slightly more advanced -fstype https://www.gnu.org/software/finduti...mono/find.html
 
Old 10-24-2017, 04:30 PM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,745

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
An attempt of an "automatic" prunefs.
The idea for excluding special file systems comes from the RH/CentOS /etc/cron.daily/mlocate.cron.
So this is actually very much like "locate".
Code:
prunefs=$( < /proc/filesystems awk '$1 == "nodev" && $2 != "zfs" { print o,"-fstype",$2; o="-o" }' )
find / \( $prunefs \) -prune -o -path '*init.d*' -print

Last edited by MadeInGermany; 10-24-2017 at 04:44 PM. Reason: added -o
 
  


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] search a directory and add file contents when find string j-me Linux - General 3 01-11-2013 08:54 AM
search string command mech123 Linux - Newbie 7 01-04-2010 10:26 PM
giving search-string(s) from a file to linux "find" command Fond_of_Opensource Linux - Newbie 3 02-02-2009 06:14 PM
Issuing find command to search for string keysorsoze Linux - Newbie 4 11-30-2007 12:06 AM
Python: search for string in a list or file chess Programming 3 08-22-2007 04:22 PM

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

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