Solved

Find and replace for all files in directory

Posted on 2004-10-14
11
256 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article will explain how to establish a SSH connection to Ubuntu through the firewall and using a different port other then 22. I have set up a Ubuntu virtual machine in Virtualbox and I am running a Windows 7 workstation. From the Ubuntu vi…
You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now