LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 03-12-2007, 11:57 AM   #1
Maeltor
Member
 
Registered: Mar 2004
Location: Boise
Distribution: CentOS 5
Posts: 69

Rep: Reputation: 15
Finding files and then finding content within those files...


Hi everyone,

I'm looking for some assistance writing the correct find command:

I need to find all files of type *System.cfg in a directory, and then i need to look into those files and find out which files contain a given string so i know which files i need to change.

We changed our time server and need to jump into all of our phone configs and change it from myfile.net to newfile.net.

I know that i can use
Code:
find / | grep *System.cfg
That will find all the files named *System.cfg in the entire file sytem structure. I only want to recursively look in a given directory and all directories beneath it.

Any help would be appreciated.
Thanks
 
Old 03-12-2007, 12:10 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,003
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
"find /" would find ALL files not just the *System.cfg ones.

The "/" in above tells it "root filesystem" (the base of all other filesystems).

To limit it do a specific subtree just specify that subtree instead of "/".

Also you don't have to grep for a name with find because it has a flag for name called appropriately enough "-name".

So to get the list of files below say /etc named *System.cfg":

Code:
find /etc -name *System.cfg
You then want to check for specific information in these files so you could pipe into xargs to do the grep for the string you're looking for in each file it finds.

Code:
find /etc/ -name *System.cfg |xargs grep <string>
Substituting your literal string value where I show <string> above.
 
Old 03-12-2007, 12:18 PM   #3
Maeltor
Member
 
Registered: Mar 2004
Location: Boise
Distribution: CentOS 5
Posts: 69

Original Poster
Rep: Reputation: 15
Hi jlightner,

Thanks for the help.

It has found the files I need. Now going off of that:

Can it just list the files containing that string? Right now it is printing off the actual operand containing the string i search for, i'd rather just have it print the name of the file containing the string.


Secondly, and I know this is getting away from the actual question, but is there a way to do a REPLACE? I'd like to just find and replace everything that says old.timeserver.net with new.timeserver.net.
 
Old 03-12-2007, 12:44 PM   #4
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,003
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
Sure you can get a list of just the files:

Code:
for FILE in `find /etc -name *System.cfg`
do if grep <string> $FILE >/dev/null
   then echo $FILE contains the string
   fi
done

To do replace (assuming these are standard ASCII text files) you can use the sed command:


Code:
for FILE in `find /etc -name *System.cfg 2>/dev/null`
do if grep <oldstring> $FILE >/dev/null
   then echo $FILE contains the string
        cp $FILE ${FILE}.20070312
        sed s/<oldstring>/<newstring>/g ${FILE}.20070312 >$FILE
   fi
done
I'd strongly urge you to try this on a test directory before doing it everywhere else to verify it has the results you want.

As always no warranty provided - try the above at your own risk.
 
Old 03-12-2007, 12:59 PM   #5
Maeltor
Member
 
Registered: Mar 2004
Location: Boise
Distribution: CentOS 5
Posts: 69

Original Poster
Rep: Reputation: 15
Ok hopefully last question:

I'm assuming those were shell scripts that you gave me?

What was the
>/dev/null in the first and the 2>/dev/null` in the second?

Also in the second, I see you have the date. What is that used for?
 
Old 03-13-2007, 12:06 PM   #6
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,003
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
Yes shell scripts. Just create text files with the content. (You can include an interpreter line if you want.)

e.g. #!/bin/bash

That would insure they run as bash shell scripts even if they were called by someone running csh for some reason. Not mandatory but a good practice.

/dev/null a/k/a "the bit bucket" is a place you can send output you don't want. Essentially it just goes away. (Sort of like where Michael Valentine sent people in Stranger in a Strange Land )

In the original >/dev/null I'm redirecting output of the command to null because I'm not interested in the output itself - just checking to make sure the command works.

In the 2>/dev/null I'm telling it to send stderr (Standard Error a/k/a File Handle 2) to /dev/null so that only stdout (Standard Output a/k/a File Handle 1) shows up.

The ">" by itself in the original by the way is sending stdout (to /dev/null - the "1" is understood so doesn't have to be listed there though it could be "1>/dev/null" if I wanted.


You can see the difference by doing (assumes you do NOT have a file named ralph):
ls -l * ralph

The above will show all your files (in stdout) AND give an error that "ralph" doesn't exist (in stderr).

If you instead do:
ls -l * ralph >/dev/null

You'll see ONLY the error message that ralph doesn't exist - all of stdout (the files that DO exist) will have been displayed to /dev/null.

If you then do:
ls -l * ralph 2>/dev/null
You'll see the list of files but NOT the error about file ralph not existing because you sent the error to /dev/null.

You can redirect into files if you wish instead of /dev/null. e.g.
ls -l * ralph >ls_stdout.out 2>ls_stderr.out
Would create the files "ls_sdtout.out" and "ls_stderr.out". You'd see all the files that exist in the first file and the error about ralph not existing in the seccond file.

Finally you can do the most common usage which is redirecting BOTH stderr and stdout to a log file:
ls -l * ralph >ls.out 2>&1
The special syntax 2>&1 says to redirect file handle 2 (stderr) into whatever has been defined for file handle 1 (stdout). Since we previously redirect stdout to ls.out it means stderr will also go to ls.out.

In this it is always important to define stdout BEFORE doing the special syntax. If you did:
ls -l * ralph 2>&1 >ls.out
It would send your stderr to stdout but stdout would not have been defined as "ls.out" yet so it would use the default (your terminal session) instead. A lot of people make this mistake so wanted to mention it.
 
  


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
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
Finding files question... tarballed Linux - General 1 03-05-2003 01:01 PM
Finding files based on content benjaminrtz Linux - General 2 08-04-2002 11:01 PM


All times are GMT -5. The time now is 05:04 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration