BOBOBK

Using find and sed to Batch Replace Strings in Text

TECHNOLOGY

How to batch replace strings in Linux? At first, I thought about using sed with -r or some iterative method to replace, but sed doesn’t have iteration parameters, so it can only be used together with find to get files and then use sed to perform replacements.

Table of Contents:

  1. Batch replace strings in text
  2. Extended usage of find
    • Find files by filename
    • Some examples of find usage
    • Advanced find and optimization
    • Find files by modification time
    • Combine with grep to find files by content
    • Find files and perform other modifications

Batch replace strings in text

Here is a direct example of finding files that contain a string and replacing it:

find . -name "*.conf" -exec sed -i 's/original/newstring/g' '{}' +;

This command consists of two parts:
The first part means to find .conf files under the current directory:

find . -name "*.conf"

The second part executes the sed command to replace strings, replacing original with newstring:

-exec sed -i 's/original/newstring/g' '{}' +

Extended usage of find

Some common uses of find:

Find files by filename

find /etc -name "*.conf"

The first argument is the directory to search, -name matches by filename, *.conf matches files ending with .conf.

Some examples of find usage

Command Description
find . -name name.txt Find file named name.txt in current directory
find /home -name *.jpg Find .jpg files under /home directory
find . -type f -empty Find empty files in current directory
find /home -user root -mtime 7 -iname “*.txt” Find .txt files under /home edited by root in last 7 days

Advanced find and optimization

By default, find doesn’t follow symbolic links. Use -L to follow symbolic links.
Find supports optimization parameters -O1, -O2, -O3 which correspond to different search strategies:

  • -O1: filename first (default)
  • -O2: filename then file type
  • -O3: highest optimization with internal sorting
Parameter Meaning
-O1 Filename priority
-O2 Filename then file type
-O3 Automatic sorting by find
-maxdepth X Max folder search depth
-iname Case insensitive filename match
-not Negation
-type f Match files
-type d Match directories

Find files by modification time

The find command can search files by modification time, which can help trace back intrusion sources besides checking logs.

find /home/wwwroot/ -name "*.php" -mtime 2

Find .php files under /home/wwwroot/ edited within the last 2 days — useful for quickly finding hacker-uploaded webshell PHP files.

Combine with grep to find files by content

Find files under a folder that contain specific content:

find /home/wwwroot/ -type f -exec grep "www.bobobk.com" '{}' ; -print

Find files under /home/wwwroot/ containing www.bobobk.com and print matching lines. Alternatively, using pipe:

find /home/wwwroot/ -type f -print | xargs grep "www.bobobk.com"

Find files and perform other file operations

This is the most important feature — find files and execute commands on them using -exec.

find . -name "nginx.conf" -exec chmod 600 '{}' ;

Find nginx.conf files and change permissions to 600. The syntax is -exec followed by the command and ends with '{}' ;.

find . -name "*.htm" -delete

Find .htm files in the current directory and delete them.

If you want to operate on directories instead of files, use -execdir:

find /home/wwwroot/default/ -type d -execdir ls '{}' ; -print

Find directories under /home/wwwroot/default/ and list files inside them.

Summary

Here we covered batch string replacement and learned various usages of the find command. This summary of the Linux file searching command helps future queries and usage. It mainly introduces searching by modification time for recent files, searching by filename, and how to operate on found files. This is crucial for batch replacing strings in files.

Reference:
Find Files in Linux, Using the Command Line

Related