Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

Find and replace for all files in directory

Hello,

I want to find all instances of "ABC123" (case sensitive) in ALL files in the directory /home/usr/stuff/ file.txt and replace all the instances with "LMNOP789"

The ownership and permissions must be preserved.

How can this be done from a command line?
I am using Red Hat 7.

Thanks!
Avatar of blkline
blkline

cd /home/usr/stuff; for f in $(ls *.txt); do cat $f | sed -e 's/ABC123/LMNOP789/g' > tmp$$.txt && chown --reference=$f tmp$$.txt; chmod --reference=$f tmp$$.txt; mv -f tmp$$.txt $f; done

Keep in mind that if the owner of the file is someone other than yourself and you're not running as root then you may not be able to overwrite the file.

Written to be more legible it looks like this:

cd /home/usr/stuff
for f in $(ls *.txt)
do
    cat $f | sed -e 's/ABC123/LMNOP789/g' > tmp$$.txt \
    && chown --reference=$f tmp$$.txt; \
    chmod --reference=$f tmp$$.txt; \
    mv -f tmp$$.txt $f
done
>I want to find all instances of "ABC123" (case sensitive) in ALL files in the directory /home/usr/stuff/  and
>replace all the instances with "LMNOP789"
>
>The ownership and permissions must be preserved.

Here's the script (one method) for that:
--------
for i in `find /home/usr/stuff/ -type f`; do vi -e -c "%s/ABC123/LMNOP789/g" -c "wq" $i; done
--------
Copy the command exactly. If you are typing, make sure all quotations are correct (Note that the first two single quotes is the key usually above your 'tab' key).
I never gave a thought to using vi in this manner.   Thanks!

Oh, make it:

find /home/usr/stuff/ -type f -name "*.txt"   as I think from the specs that the filenames all have a txt extension.
You are welcome. BTW, FYI, the filenames need not necessarily have a txt extension.
Avatar of hankknight

ASKER

Thanks!  It worked, but it returned several pages of errors:

                   Error detected while processing command line:
                   Pattern not found: storefront

For future refference, is there a way to supress the errors?

Thanks!
You get the errors because some of the "ALL files in the directory" don't
contain the source pattern for the replace.  Rather than 'find', why don't
you use 'grep -l' to limit the files to those that contain the source pattern.

for i in `grep -l "ABC123" /home/usr/stuff/*.txt`; do vi -e -c "%s/ABC123/LMNOP789/g" -c "wq" $i; done

SOLUTION
Avatar of Luxana
Luxana
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of yuzh
yuzh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry about the typo, it should be "xargs", Sunjith thanks for the correction.
I'd use grep a bit differently:

for f in $(find /usr/home/stuff -type f) ; do grep ABC123 $f && cat $f | sed "s/ABC123/LMNOP789/g" > tmp$$.txt && chown --reference=$f tmp$$.txt; mv -f tmp$$.txt $f; done