LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 03-10-2010, 03:28 PM   #1
Lharrypersaud
LQ Newbie
 
Registered: Apr 2008
Posts: 6

Rep: Reputation: 0
Linux Searching


If i am in the root directory and i need to search for a specific file in the sysconfig directory, is there any way to search this directory for a file?
 
Old 03-10-2010, 03:31 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Sure, you can use find--
Code:
find /etc/sysconfig -iname filename -print
If you've got locate/slocate/mlocate on your system you can also type:

Code:
locate filename
Locate is typically faster than find over a large structure, but requires you to generally crontab the updatedb program to run daily/weekly/monthly... and it's results are only as recent as the last time the updatedb ran.

Last edited by rweaver; 03-10-2010 at 03:33 PM.
 
Old 03-10-2010, 03:39 PM   #3
Lharrypersaud
LQ Newbie
 
Registered: Apr 2008
Posts: 6

Original Poster
Rep: Reputation: 0
I am sorry but i didn't explain myself properly.
Let's say i don't know where the file is located but i know the name of the file?
 
Old 03-10-2010, 03:54 PM   #4
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
Just adapt the commands above or, as suggested, try locate.

The man pages are always the best place to look; man find
 
Old 03-10-2010, 04:08 PM   #5
Lharrypersaud
LQ Newbie
 
Registered: Apr 2008
Posts: 6

Original Poster
Rep: Reputation: 0
That worked

I greatly appreciate that.

Thank you very much
 
Old 03-11-2010, 09:06 AM   #6
zrdc28
Member
 
Registered: Dec 2007
Location: Alabama USA
Distribution: Slackware current
Posts: 300

Rep: Reputation: 53
I very seldom know the exact name of a file so I use "krusader" to do my file search. Just open krusader, go to "tools" in the top toolbar. Click on search. If the file name is "johnwenttotown" just type in john, on the second line type in"/" to search all. It will give you every file that starts with john and what directory they are in. If you click on it, it will take you to that file.
 
Old 03-11-2010, 04:53 PM   #7
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by Lharrypersaud View Post
I am sorry but i didn't explain myself properly.
Let's say i don't know where the file is located but i know the name of the file?
It's exactly the same commands you just change the base path for the find and the locate would remain exactly the same--
Code:
find / -iname filename -print
Which will search the entire drive for "filename" without case sensitivity.

If you knew it was somewhere in /etc then...
Code:
find /etc -iname filename -print
 
Old 03-11-2010, 07:13 PM   #8
mckinnon81
LQ Newbie
 
Registered: Feb 2010
Location: Brisbane, QLD
Distribution: Ubuntu, Debian
Posts: 7

Rep: Reputation: 0
I find the easiest way to do this is by using the following, its quick and dirty and it works

This will search the entire folder structure

Code:
find / | grep <what.we.are.looking.for>

eg

user / $  find / | grep conf | more
To search just the current folder you are in

Code:
find . | grep <what.we.are.looking.for>

eg

user ~/Documents $  find . | grep doc | more
Use the more for page viewing if a long list. Otherwise you can remove it.

Am I wrong in doing it this way?

It works so I am not complaining.
 
Old 03-11-2010, 08:59 PM   #9
David2010
Member
 
Registered: May 2009
Posts: 255

Rep: Reputation: 23
I just always did this: sudo find / -name 'nameoffile'

or if you are looking for a program's install directory type this:

which nameofprogram

It helps if you are root!
 
Old 03-11-2010, 10:20 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
If you only know part of the filename you can use wildcards with find (piping find's output to grep is a less efficient way of doing the same thing). If it's an ordinary file (not a directory, a device file ...) then you can speed find along by specifying so. If you are not running the find as root then find will generate a lot of error messages when it can't enter directories; you don't need to see these because you can't reach any files in those directories. Combining all the above
Code:
find / -type f -iname '*foo*' 2>/dev/null
The single quotes around *foo* are required in case there are any files matching *foo* in the current directory.
 
Old 03-12-2010, 10:23 AM   #11
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by mckinnon81 View Post
I find the easiest way to do this is by using the following, its quick and dirty and it works
...<SNIP>...
Use the more for page viewing if a long list. Otherwise you can remove it.

Am I wrong in doing it this way?

It works so I am not complaining.
You're not wrong, but you miss some of the more advanced features of find...

Code:
find /var/www -type f -iname *.htm -exec chown apache:apache {} \&\& chmod 644 {} \;
find /var/www -type d -exec chown apache:apache {} \&\& chmod 755 {} \;
Will find any file with .htm under /var/www and chown it to apache:apache and change the perms to 644. Then change every directory under /var/www to 755 and chown them to apache:apache also.

You can accomplish this with piping also but its generally far easier using the advanced structures of find... and for me personally, "-iname filename" or " | grep filename" I'll take the one with the lesser amount of typing, plus to make grep case insensitive you'd also have to add -i which makes it longer yet. Economy of typing.

Honestly though, that's one of the great things about unix, there are 10 different ways you can go about many tasks.
 
1 members found this post helpful.
  


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
Searching for Linux Codecs gehiii Linux - Software 5 02-02-2010 08:35 PM
searching google for linux aot2002 Linux - Enterprise 1 05-31-2006 12:42 AM
Searching files on Linux dasoslukos Linux - Software 2 10-25-2004 12:12 AM
Why is searching in linux so slow? Braveheart1980 Linux - General 9 02-26-2004 09:15 PM
Searching linux-guide cicero Linux - Hardware 2 05-13-2003 05:48 PM

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

All times are GMT -5. The time now is 04:27 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration