General discussion

Locked

Scan for files and replace string?

By resch ·
What is a foolproof way to scan a directory structure for files containing certain text strings and
1. produce a list of all the files containing the string and/or
2. replace the string with another string

The script (or whatever solution is available) should ignore links made with "ln -s" (I'm aware of grep and find, but find tells me that there are too many nested links).

Must work on FreeBSD.

This conversation is currently closed to new comments.

13 total posts (Page 1 of 2)   01 | 02   Next
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Scan for files and replace string?

by trace In reply to Scan for files and replac ...

you can use "find" command like follows:
find /dir -name "*str*" ! -type l -print

Collapse -

Scan for files and replace string?

by resch In reply to Scan for files and replac ...

The question was auto-closed by TechRepublic

Collapse -

Scan for files and replace string?

by zaruba In reply to Scan for files and replac ...

find /DIR \! -type l -exec grep -l STRING \;

Collapse -

Scan for files and replace string?

by resch In reply to Scan for files and replac ...

The question was auto-closed by TechRepublic

Collapse -

Scan for files and replace string?

by gordon In reply to Scan for files and replac ...

This should work, if I've interpreted it right: ls -l | grep -v "^l" | grep "string"
ls -l "long" directory listing with
"-rwxrwxrwx" as first "field"
grep -v "^l" will take pipe input and exclude (-v) all items that have an "l" (Lowercase L) as the first position
grep "string" will take the results of the second pipe and look for "string".

To replace with another string, be very careful, b/c if the other string is malformed, you may mess up the wrong thing (Voice of exp. here!) Ok, this is more difficult (This is shell script):
#!/bin/ksh

ls -l | grep -v "^l" | grep "string" | awk '{print $9}' > tempfile
# Awk stmt to print filename only.
sed 's/string/replace/' tempfile > tempfile1
# sed to create replacement filename.
for modfilename in `grep "^" tempfile1`
do
for filename in `grep "^" tempfile`
do
echo $filename $modfilename
# replace with:
# mv $filename $modfilename
#when you're confident this will work.
done
done
Goes w/o saying to teston dummy files fi

Collapse -

Scan for files and replace string?

by resch In reply to Scan for files and replac ...

The question was auto-closed by TechRepublic

Collapse -

Scan for files and replace string?

by ray In reply to Scan for files and replac ...

I would go along with answer #2 with a few changes. I think something like this would provide fool proof results:

find /DIR -type f -exec grep -l STRING {} \;

also add a "-i" to the grep if you want it to be case insensitive.

as for the replacement of the string I would use a heredoc and "ex"

Collapse -

Scan for files and replace string?

by resch In reply to Scan for files and replac ...

The question was auto-closed by TechRepublic

Collapse -

Scan for files and replace string?

by david_totsch In reply to Scan for files and replac ...

I don't have access to FreeBSD, but read the man(1) page on find(1). You should be able to select based on the "type" of the file. The first inclination would be to avoid sybolic links:

find . ! -type l

But, you could wind up doing somethingnasty like running grep(1) against a named pipe. Use positive logic looking for "regular files":

find . -type f

This will prevent you from wasting time grepping directories, character files and the like.

Now, you want to form a list of files that contain the string you are looking for. You have two choices:

1) find . -type f -exec grep "string" {} \;

or

2) find . -type f | xargs grep "string"

Number one will spaw a grep for every file found by find(1). Spawing a new process is one of the most expensive UNIX operations. So, you might consider number two. The second option uses xargs to spawn fewer copies of grep, however, you may bump up againt an arguments length limit.

Either way, grep is going to return lines that contain the fi

Collapse -

Scan for files and replace string?

by resch In reply to Scan for files and replac ...

The question was auto-closed by TechRepublic

Back to Linux Forum
13 total posts (Page 1 of 2)   01 | 02   Next

Related Discussions

Related Forums