How to use the sed command to replace a text in files present in a directory and subdirectories?

Let's consider a case where we have two directories, say, d1 and d2 and both these directories contain some files, which may be the same or different. Now we want to make use of the sed command to replace a particular text that might be present in some of the files in either the d1 directory or the d2 directory.

The sed command, which is short for stream editor, is used to perform different functions like find, replace, insert and many more on a particular file. When combined with the find command, it becomes a powerful tool for batch text replacement across multiple files in directories and subdirectories.

Directory Structure Example

Let's explore a case where we have two directories d1 and d2 and both these directories contain some files in them.

Consider the terminal output to depict these two directories shown below −

immukul@192 linux-questions-code % ls -ltr
total 0
drwxr-xr-x 5 immukul staff 160 Jul 5 20:03 d1
drwxr-xr-x 4 immukul staff 128 Jul 5 20:03 d2

Now the contents of the first directory d1 looks something like this −

immukul@192 d1 % ls -ltr
total 0
-rw-r--r-- 1 immukul staff 0 Jul 5 20:03 1.txt
-rw-r--r-- 1 immukul staff 0 Jul 5 20:03 3.txt
-rw-r--r-- 1 immukul staff 0 Jul 5 20:03 5.txt

Now the contents of the second directory d2 looks something like this −

immukul@192 d2 % ls -ltr
total 0
-rw-r--r-- 1 immukul staff 0 Jul 5 20:03 2.txt
-rw-r--r-- 1 immukul staff 0 Jul 5 20:03 4.txt

File Contents Before Replacement

If we print the content of the files that are present inside the d1 directory we will get something like this −

immukul@192 d1 % cat 1.txt
orange

immukul@192 d1 % cat 3.txt

immukul@192 d1 % cat 5.txt
orange

We can see that the files 1.txt and 5.txt contain a string 'orange' in them.

Now if we print the content of the files that are present inside the d2 directory we will get something like this −

immukul@192 d2 % cat 2.txt
orange

immukul@192 d2 % cat 4.txt

We can see that the file 2.txt contains a string 'orange' in it.

Using find with sed for Text Replacement

Now we want to replace the string 'orange' with 'apple' in all the files using a single command. To do that we use the following command −

find ./ -type f -exec sed -i -e 's/orange/apple/g' {} \;

Command Breakdown

Component Description
find ./ Search in current directory and subdirectories
-type f Find only regular files (not directories)
-exec Execute a command on each found file
sed -i Edit files in-place (modify original files)
s/orange/apple/g Substitute 'orange' with 'apple' globally
{} Placeholder for the filename found by find
\; Terminate the -exec command

Output

After running the command, the file contents are updated as shown below −

immukul@192 d1 % cat 1.txt
apple

immukul@192 d1 % cat 3.txt

immukul@192 d1 % cat 5.txt
apple

immukul@192 d2 % cat 2.txt
apple

immukul@192 d2 % cat 4.txt

Alternative Commands

You can also use specific file patterns or restrict the search to certain directories −

# Replace only in .txt files
find ./ -name "*.txt" -exec sed -i 's/orange/apple/g' {} \;

# Replace in specific directory
find ./d1/ -type f -exec sed -i 's/orange/apple/g' {} \;

Conclusion

The combination of find and sed commands provides an efficient way to perform text replacement across multiple files in directories and subdirectories. The -exec option allows sed to process each file found by find, making it a powerful tool for batch text processing operations.

Updated on: 2026-03-17T09:01:38+05:30

869 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements