LinuxQuestions.org
Review your favorite Linux distribution.
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 05-15-2008, 10:40 PM   #1
Sunfist
Member
 
Registered: Jun 2007
Location: Washington
Distribution: Ubuntu 12.04
Posts: 37

Rep: Reputation: 15
Finding files


Is there a good utility to find files when you have no idea exactly where they are? Secondly what does Linux use for wildcard chars in a search, for example in Windows you use * or ? , is it the same in Linux?
 
Old 05-15-2008, 10:57 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Wildcards:
* matches any number of chars, including none.
? matches one char.

The utilities to find files:
locate: requires an up-to-date database, which is built by the command updatedb. Then, if you know a string of letters of the filename (a regular expresion, or regex), the command is 'locate <string>' for a case sensive search for that string, and 'locate -i <string>' for a case in-sensitive search.

find: If you know part of the filename or part of the contents of the file, use the find command, and pipe it through grep to search in filenames and file contents.
find / -type f | grep <string>, where / begins the search at the root of the filesystem. You can reduce the search by giving the directory to search, as in 'find /home -type f | grep <string>' to restrict the search to the /home directory.
find / -type f | grep -i <string>, does the same except the search is case in-sensitive.

In the examples of find given above, the find command is piped through grep to search for the string.

For both commands, see the man pages: 'man locate' and 'man find' for more information.

Last edited by bigrigdriver; 05-15-2008 at 11:00 PM.
 
Old 05-15-2008, 11:21 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I would add a small clarification. There are two types of wild cards. The ? and * characters are used in the shell and for the -name argument of the find command. For real regular expressions ( used in grep, locate --regex, sed, etc. ) the dot (.) character is used instead of ?, and the * metacharacter means zero or more instances of the preceding charactor (or regular expression).

Code:
find /home/jschiwal/Documents/ -iname "abs*.pdf"
/home/jschiwal/Documents/abs-guide.pdf

> locate --regex '/usr/.*gawk\.pdf$'
/usr/src/packages/BUILD/gawk-3.1.5g/doc/gawk.pdf
Since the dot character is a metacharacter it needs to be escaped if meant literally as in the above example "/usr/.*gawk\.pdf$'

See the find info manual for numerous examples of using the find command and of regular expressions. There is also a "man 7 regex" manpage.
 
Old 05-15-2008, 11:22 PM   #4
Sunfist
Member
 
Registered: Jun 2007
Location: Washington
Distribution: Ubuntu 12.04
Posts: 37

Original Poster
Rep: Reputation: 15
Will find search all the directories, or only the one you are in when you run it?
 
Old 05-15-2008, 11:37 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Its recursive ie all dirs under the specified one (plus inside the specified one)
 
Old 05-15-2008, 11:48 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can use the '-maxdepth 1' argument to control the levels of recursion (to 1 in this case). It seems like you haven't read the find man page yet. That will answer a lot of questions. The find command may be the most useful command you will use.

Last edited by jschiwal; 05-19-2008 at 08:02 AM. Reason: fixed typo
 
Old 05-16-2008, 10:12 AM   #7
Frank Soranno
Member
 
Registered: Jun 2005
Location: Scranton, Pa. USA
Distribution: Debian
Posts: 230

Rep: Reputation: 30
Quote:
Originally Posted by bigrigdriver View Post
Wildcards:
* matches any number of chars, including none.
? matches one char.

The utilities to find files:
locate: requires an up-to-date database, which is built by the command updatedb. Then, if you know a string of letters of the filename (a regular expresion, or regex), the command is 'locate <string>' for a case sensive search for that string, and 'locate -i <string>' for a case in-sensitive search.

find: If you know part of the filename or part of the contents of the file, use the find command, and pipe it through grep to search in filenames and file contents.
find / -type f | grep <string>, where / begins the search at the root of the filesystem. You can reduce the search by giving the directory to search, as in 'find /home -type f | grep <string>' to restrict the search to the /home directory.
find / -type f | grep -i <string>, does the same except the search is case in-sensitive.

In the examples of find given above, the find command is piped through grep to search for the string.

For both commands, see the man pages: 'man locate' and 'man find' for more information.
frank@c-75-75-174-33:~$ updatedb
bash: updatedb: command not found
frank@c-75-75-174-33:~$ man locate
No manual entry for locate

Last edited by Frank Soranno; 05-16-2008 at 10:13 AM. Reason: Adding
 
Old 05-16-2008, 10:44 AM   #8
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Quote:
frank@c-75-75-174-33:~$ updatedb
bash: updatedb: command not found
Normally you need to be root to run updatedb. Most of us have it run as a cron job.
But...
Quote:
frank@c-75-75-174-33:~$ man locate
No manual entry for locate
It looks as though you do not locate installed
Consult your packagemanager. For my distro, locate comes as part of the findutils package
 
Old 05-16-2008, 11:10 AM   #9
Frank Soranno
Member
 
Registered: Jun 2005
Location: Scranton, Pa. USA
Distribution: Debian
Posts: 230

Rep: Reputation: 30
Quote:
Originally Posted by tredegar View Post
Normally you need to be root to run updatedb. Most of us have it run as a cron job.
But...
It looks as though you do not locate installed
Consult your packagemanager. For my distro, locate comes as part of the findutils package
Thanks
I'm trying to on Debian forum right now, Got inpatient and tried this forum.
 
  


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
finding files nmansour Linux - Newbie 9 08-06-2007 09:11 AM
Finding files and then finding content within those files... Maeltor Linux - Software 5 03-13-2007 12:06 PM
finding files in C++ poeta_boy Programming 2 07-11-2004 01:25 AM
Finding files brentos Linux - General 3 03-22-2004 08:24 PM
Finding files Nicksan Linux - Software 3 06-30-2003 07:16 PM

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

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