LinuxQuestions.org
Review your favorite Linux distribution.
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 01-31-2007, 07:26 AM   #1
nesta
Member
 
Registered: Aug 2006
Posts: 100

Rep: Reputation: 15
how to search a file?


hi folks
i want to search for a certain file in all the folders and sub-folders?
what is the shell-script command that enables me to do that?
also if i want to change something in it too?
 
Old 01-31-2007, 07:38 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

If your system runs updatedb regularly you can use locate <whatever>. It does depend on a database that reflects the current situation.

If locate is not an option, you can use find. Find is a bit resource unfriendly.

find . -name "filenamegoeshere" -type f -print

The above command searches for a file (-type f) called filenamegoeshere and prints it. You can use wildcards (-name "*lenamegoesh*"). The command start looking from the directory you are in (the .(dot)). If you want to start from a fixed directory you can use this as well (find /home/user ......). Find is recursive.
 
Old 01-31-2007, 07:39 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
See man find. A search based on the file name is
Code:
find . -name filename
This start the search from the current directory ".".
To execute some commands on the files, look at the -exec option.

Edit: beated from the more detailed druuna's post!

Last edited by colucix; 01-31-2007 at 07:43 AM.
 
Old 01-31-2007, 09:24 AM   #4
nesta
Member
 
Registered: Aug 2006
Posts: 100

Original Poster
Rep: Reputation: 15
thanks for ur help but
i can get the file that i m searching about it using the find command as follows:
code:
PHP Code:
find . -name "changes.txt" 
but now i want to replace a certain word in it with another string.
actually should i make it using sed or using -exec option of find command.

i have tried this on the shell but i got error:
PHP Code:
find . -name "changes.txt" sed s/student/Engineerin 
the error is:
PHP Code:
sed: -e expression #1, char 18:unterminated 's' command 
thanks in advance
 
Old 01-31-2007, 09:38 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Find with the -exec option:

find . -name "changes.txt" -exec sed -i 's/Student/Engineer/' {} \;

The change is done 'in place' (sed's -i option, only works in sed 4.*).

Find and xargs:

find . -name "changes.txt" -print | xargs sed -i 's/Student/Engineer/'

The last command is supposed to be faster, even though I like the first one better
 
  


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
File Search rjohnson244 Linux - Software 4 09-26-2004 01:41 PM
Search for File corley Linux - General 2 08-04-2004 08:40 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
How to search for a file montejr Linux - General 5 03-08-2004 01:12 PM

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

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