LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 12:25 PM   #1
peter7089
Member
 
Registered: May 2016
Distribution: MX Linux
Posts: 249

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, 12:27 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,103

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

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, 01:40 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,084
Blog Entries: 3

Rep: Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665
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, 02:15 PM   #5
peter7089
Member
 
Registered: May 2016
Distribution: MX Linux
Posts: 249

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, 02:48 PM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,084
Blog Entries: 3

Rep: Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665
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, 02:50 AM   #7
peter7089
Member
 
Registered: May 2016
Distribution: MX Linux
Posts: 249

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, 03:03 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,084
Blog Entries: 3

Rep: Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665Reputation: 3665
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, 06:10 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,972

Rep: Reputation: 5677Reputation: 5677Reputation: 5677Reputation: 5677Reputation: 5677Reputation: 5677Reputation: 5677Reputation: 5677Reputation: 5677Reputation: 5677Reputation: 5677
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, 02:35 PM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052
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, 02:50 PM   #11
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 997

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
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, 05:30 PM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,632

Rep: Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145
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 05: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 09:54 AM
search string command mech123 Linux - Newbie 7 01-04-2010 11:26 PM
giving search-string(s) from a file to linux "find" command Fond_of_Opensource Linux - Newbie 3 02-02-2009 07:14 PM
Issuing find command to search for string keysorsoze Linux - Newbie 4 11-30-2007 01:06 AM
Python: search for string in a list or file chess Programming 3 08-22-2007 05:22 PM

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

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