Solved

Find and replace for all files in directory

Posted on 2004-10-14
11
260 Views
Last Modified: 2013-12-06
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!
0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
11 Comments
 
LVL 6

Expert Comment

by:blkline
ID: 12310824
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
0
 
LVL 2

Expert Comment

by:Sunjith
ID: 12311009
>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).
0
 
LVL 6

Expert Comment

by:blkline
ID: 12311050
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.
0
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
LVL 2

Expert Comment

by:Sunjith
ID: 12311474
You are welcome. BTW, FYI, the filenames need not necessarily have a txt extension.
0
 
LVL 16

Author Comment

by:hankknight
ID: 12311637
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!
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12313555
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

0
 
LVL 10

Assisted Solution

by:Luxana
Luxana earned 100 total points
ID: 12314078
> For future refference, is there a way to supress the errors?

you can redirect standart error to /dev/null

lets take blkline's example :

find /home/usr/stuff/ -type f -name "*.txt" 2> /dev/null

so all errors will be send to /dev/null and you get just STDOUT
0
 
LVL 38

Assisted Solution

by:yuzh
yuzh earned 100 total points
ID: 12314656
How about:

(find /home/usr/stuff/ -type f | xarg vi -e -c "%s/ABC123/LMNOP789/g" -c "wq" ) 2>/dev/null

please credit to  Sunjith's vi statement, you can also use perl/sed to do the same job.
0
 
LVL 2

Accepted Solution

by:
Sunjith earned 300 total points
ID: 12315135
> (find /home/usr/stuff/ -type f | xarg vi -e -c "%s/ABC123/LMNOP789/g" -c "wq" ) 2>/dev/null

That's good. Just a correction. It should be "xargs" instead of "xarg"

Grepping is a better idea than find because it filters out the files needed to be processed by vi and grep processes file much faster than vi. Combining all the ideas, the better command would be:
--------
grep -srl "ABC123" /home/usr/stuff/ | xargs vi -e -c "%s/ABC123/LMNOP789/g" -c "wq"
--------

Though there wouldn't be too many errors as you got with the previous script, if you are very particular, you may redirect the std error as well:
--------
grep -srl "ABC123" /home/usr/stuff/ | xargs vi -e -c "%s/ABC123/LMNOP789/g" -c "wq" 2>/dev/null
--------
0
 
LVL 38

Expert Comment

by:yuzh
ID: 12315163
Sorry about the typo, it should be "xargs", Sunjith thanks for the correction.
0
 
LVL 4

Expert Comment

by:EinarTh
ID: 12317537
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

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question