LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 02: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, 02:31 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
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 02:33 PM.
 
Old 03-10-2010, 02: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, 02:54 PM   #4
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 70
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, 03: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, 08:06 AM   #6
zrdc28
Member
 
Registered: Dec 2007
Location: Alabama USA
Distribution: Slackware current
Posts: 309

Rep: Reputation: 55
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, 03:53 PM   #7
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
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, 06: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, 07: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, 09:20 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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, 09:23 AM   #11
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
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



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 07:35 PM
searching google for linux aot2002 Linux - Enterprise 1 05-30-2006 11:42 PM
Searching files on Linux dasoslukos Linux - Software 2 10-24-2004 11:12 PM
Why is searching in linux so slow? Braveheart1980 Linux - General 9 02-26-2004 08:15 PM
Searching linux-guide cicero Linux - Hardware 2 05-13-2003 04:48 PM

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

All times are GMT -5. The time now is 10:09 AM.

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