LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2010, 02:40 AM   #1
Volcano
Member
 
Registered: Sep 2005
Posts: 225

Rep: Reputation: 15
How to search for a file ?


How to search for a file ?

I am looking for a file in a directory /shared/domain

This also contains many sub directories .

I need a script which will let me know the location of my log file myapps.log.

what is the script ?

Can it be made in one line ...because I just want to run ...don't want to save the script.
 
Old 02-03-2010, 02:42 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

You don't need a script for that. Just have a look at the man page for find and you'll 'find' what you need.
Code:
man find
Kind regards,

Eric
 
Old 02-03-2010, 02:43 AM   #3
Volcano
Member
 
Registered: Sep 2005
Posts: 225

Original Poster
Rep: Reputation: 15
does 'find' command can search for a specific file from directories ?

can you tell me what to type if i want to search for a file myapps.log

Last edited by Volcano; 02-03-2010 at 03:01 AM.
 
Old 02-03-2010, 02:48 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Code:
find . -name 'yourfilename'
will search for the file with name yourfilename in the current directory listing the location in subdirectories relevant to the current directory.

You should really look at the man page of find since there are a lot of options you can use to find your files. Find is one of the basics in Linux, so it's best you get acquainted with it.

Kind regards,

Eric
 
Old 02-03-2010, 03:04 AM   #5
Volcano
Member
 
Registered: Sep 2005
Posts: 225

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by EricTRA View Post
Code:
find . -name 'yourfilename'
will search for the file with name yourfilename in the current directory listing the location in subdirectories relevant to the current directory.

You should really look at the man page of find since there are a lot of options you can use to find your files. Find is one of the basics in Linux, so it's best you get acquainted with it.

Kind regards,

Eric
This does not work.

To Test I did ...
find . -name 'ms01.log' ....no matches found.

Last edited by Volcano; 02-03-2010 at 03:08 AM.
 
Old 02-03-2010, 03:18 AM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by Volcano
How to search for a file ?
Did you try :
Code:
locate myapps.log
locate command will be more useful when used along with the regular expressions.
 
Old 02-03-2010, 03:28 AM   #7
Volcano
Member
 
Registered: Sep 2005
Posts: 225

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by anishakaul View Post
Did you try :
Code:
locate myapps.log
locate command will be more useful when used along with the regular expressions.
locate is not a working command in my box.
 
Old 02-03-2010, 03:28 AM   #8
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Quote:
Originally Posted by Volcano View Post
This does not work.

To Test I did ...
find . -name 'ms01.log' ....no matches found.
Are you sure the file is supposed to be in the directory where you executed the command? Or in a subdirectory of that one? The point (.) means that it will only look in the current directory (where you run the command) and the subdirectories it holds.

If you want to look system wide then use:
Code:
find / -name 'ms01.log'
Kind regards,

Eric
 
Old 02-03-2010, 03:32 AM   #9
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by Volcano
locate is not a working command in my box.
Kindly name the distribution and its version u are using !
 
Old 02-03-2010, 04:00 AM   #10
Volcano
Member
 
Registered: Sep 2005
Posts: 225

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by EricTRA View Post
Are you sure the file is supposed to be in the directory where you executed the command? Or in a subdirectory of that one? The point (.) means that it will only look in the current directory (where you run the command) and the subdirectories it holds.

If you want to look system wide then use:
Code:
find / -name 'ms01.log'
Kind regards,

Eric
when I run this system wide search ...I get many "Permission denied" message.

message is : find: cannot read dir directory_name_here Permission denied.

However, I just want to find the location of the file .

I can browse into the filesystem and can download this file using Filezilla easily !

can we get rid of those permission denied messages ? this is making the screen clumsy.
 
Old 02-03-2010, 04:12 AM   #11
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
Quote:
Originally Posted by Volcano View Post
can we get rid of those permission denied messages ? this is making the screen clumsy.
Sure
Code:
find / -name 'ms01.log' 2>/dev/null
If you are looking for an ordinary file, not a directory, not a "special" file such as a "device file" etc., you can tell find using -type f. If you are not sure of the uppercase/lowercase file name you can tell find to ignore case by using -iname instead of name. Hence
Code:
find / -type f -iname 'ms01.log' 2>/dev/null
The find man page is one of the hardest to understand so you may be better off looking at a few examples before tackling the man page directly.
 
Old 02-03-2010, 04:20 AM   #12
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Quote:
Originally Posted by Volcano View Post
when I run this system wide search ...I get many "Permission denied" message.

message is : find: cannot read dir directory_name_here Permission denied.

However, I just want to find the location of the file .

I can browse into the filesystem and can download this file using Filezilla easily !

can we get rid of those permission denied messages ? this is making the screen clumsy.
Looks like you're running the command as normal user. Either run it as root or use sudo to execute the command.
Code:
sudo find / -name 'ms01.log'
Or use the solution offered by catkin which sends the error messages into cyberspace.

Kind regards,

Eric
 
Old 02-03-2010, 05:40 AM   #13
Volcano
Member
 
Registered: Sep 2005
Posts: 225

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
Sure
Code:
find / -name 'ms01.log' 2>/dev/null
If you are looking for an ordinary file, not a directory, not a "special" file such as a "device file" etc., you can tell find using -type f. If you are not sure of the uppercase/lowercase file name you can tell find to ignore case by using -iname instead of name. Hence
Code:
find / -type f -iname 'ms01.log' 2>/dev/null
The find man page is one of the hardest to understand so you may be better off looking at a few examples before tackling the man page directly.
what is ' 2>/dev/null' ? is not 'dev' is a directory ? is 'null' also a directory ? I never heard a directory named 'null'! .....is it storing any file ?

I don't understand this ...can you please explain this part. I would like to test this command .
 
Old 02-03-2010, 05:46 AM   #14
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hi,

/dev/null is the 'bit bucket', the 'waste basket', 'the garbage can', and so on. When you give a 2>/dev/null at the end of a command you're redirecting (>) the error output to that waste basket instead of to 'stdout' (standard output which is your console).

Kind regards,

Eric
 
Old 02-03-2010, 07:48 PM   #15
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
Just expand on /dev/null, its a 'virtual' file; what really happens is that anything sent to /dev/null is thrown away by the OS, ie not saved to disk (or anywhere else).
You should really get in the habit of reading the man pages as suggested. Its a common idiom, and not just for find.
See for example any of the bash scripting howtos.
http://linux.die.net/man/1/find
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
  


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
How to do search & replace on a text file--need to extract URLs from a sitemap file Mountain Linux - General 4 08-07-2015 10:52 AM
How to search "file:///" in internet search engines? tirengarfio General 4 05-23-2009 07:56 PM
How to read string in 1 file and search in another file? ameyapandit Programming 7 07-11-2008 12:05 PM
Find File broken, need search utility, where does WineX install, KDE file roller? Ohmn Mandriva 6 07-05-2004 10:34 PM
How to 'apt-cache search' & 'apt-file search' by distribution? davidas Debian 3 04-19-2004 01:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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