Bulk replacement file content method under Linux

Source: Internet
Author: User

1: Find
find.-type f-name "*.html" |xargs grep ' yourstring '

2: Find and replace
find-name ' filename ' to find ' | xargs perl-pi-e ' s| replaced string | replaced string |g '

perl-pi-e
By adding the- e option to the Perl command followed by a line of code, it runs the code as if it were running a normal Perl script.
using Perl from the command line can help implement some powerful, real-time transformations. Careful study of regular expressions and their proper use will save you a lot of manual editing work.

3: Bulk Modify folder Permissions
find.-type-d-name *.html|xargs chmod 755

4: Bulk Modify file Permissions
find.-type-f-name *.html|xargs chmod 644


finding and replacing is a very common operation.

The little tricks introduced here can make it easy to do a lot of repetitive, tedious work.

take an example to illustrate

Find the string "password" in the. c file in the current directory
grep "Password" *.c

find files in the current directory and its subdirectories test.c
find.-name "test.c"-print

find the. vbs file in the current directory and its subdirectories and delete it
find.-name "*.vbs"-exec rm {} \;

Find the string "password" in the. c file in the current directory and its subdirectories
find.-name "*.c"-print | xargs grep "password"

Replace the string "password" with "pwd" in the. c file in the current directory.
perl-pi-e ' s/password/pwd/g ' *.c

Replace the string "password" with "pwd" in the. c file in the current directory and back up with a. bak extension
perl-pi.bak-e ' s/password/pwd/g ' *.c

Replace the string "password" as "pwd" in the. c file in the current directory and subdirectories
find.-name "*.c"-print | xargs perl-pi-e ' s/password/pwd/g '

use SED to manipulate bulk replacements

format: sed-i "s/lookup field/replace field/g" ' grep lookup field-rl path '

linux SED Bulk replaces strings in multiple files

sed-i "s/oldstring/newstring/g" ' grep oldstring-rl yourdir '

For example: Replace the www.viiving.com in all files under/home to www.zjant.com

sed-i "s/www.viiving.com/www.zjant.com/g" ' grep www.viiving.com-rl/mysites

operation in current directory: Sed-i "s/www.viiving.com/www.zjant.com/g" *

Bulk replacement file content method under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.