How to Find and Replace text Strings on Linux
Finding and replacing a text string in the file is one of the most basic text editing operations. All text editors support this operation. You can use the text editor's built-in feature or a separate command to find and replace a text string in the file. The first option is good if the file is open for editing. The second option is good if the file is closed. In this tutorial, we will learn about both options. We will use GUI mode to understand the first option and CLI mode for the second mode.
Finding and replacing text strings on the command line
We can use the sed command to find and replace text strings in the file on the command line. The sed command allows us to search for occurrences of a text string and then replace the text string. It uses the following syntax.
$sed s/exiting_string/new_string/g file_name > new_file_name
The following table explains the parameters of the above syntax.
| sed | Main command. |
| s | Command's option to search and replace a text string. |
| exiting_string | The text string that we want to replace. |
| new_string | The string we want to use instead of the current text string. |
| g | Command's option to perform the replace action in the entire file. We can also perform the replace operation for a specific occurrence of the text string. To replace a particular occurrence of the text string, use the place number of the occurrence. For example, if you want to replace the third occurrence of the text string, use the number 3 in the place of the letter 'g'. |
| file_name | File name including the path. |
| > | Shell's feature to redirect the output of a command to another command or store the output as a new file. |
| new_file_name | Name of the new file. If you want to store the modified file as the new file, you can specify the name of the new file. If you save the changes in the new file, the command will not change the original file. |
Let's take an example.
The following command replaces the second occurrence of the text string 'ocean' with the text string 'sea' in the file 'ocean.txt'.
$sed s/ocean/sea/2 ocean.txt
The following image shows the output of the above command.

The following command replaces all occurrences of the text string 'ocean' with 'sea' and saves the modified file as a new file 'sea.txt'. The original file remains unchanged.
$sed s/ocean/sea/g ocean.txt > sea.txt
The following image shows the output of the above command.

The following command replaces all occurrences of the text string 'ocean' with the text string 'sea' in the file 'ocean.txt'.
$sed s/ocean/sea/g ocean.txt
The following image shows the output of the above command.

Finding and replacing text strings on GUI
To find and replace text strings on GUI, open the file where you want to replace text strings, click the Edit menu item and click the Replace tool. You can also use a combination of shortcut keys to access this tool. Different text editors assign different shortcut keys to access this tool. For example, the Libra office assigns Ctrl + H keys to this tool. To access this tool directly, you can press Ctrl + H keys. This tool contains control options and two input boxes: Find and Replace. Type the text string you want to remove in the Find box and the text string you want to add in the Replace box.
You can replace all or some occurrences of the string. To replace all occurrences, click the Replace All button. To replace only the first occurrence, click the Replace button. Click the Find until you reach the desired occurrence to replace a specific occurrence. By default, the tool performs the replace operation in the forward direction. To perform this operation in the backward direction, click the 'Replace backward' option. You can also use regular expressions to perform more specific replacements.
Let's take an example. Suppose we have a file that contains a text string 'sea'. We want to replace this string with the string 'ocean'. To perform this operation, open the file and press the Ctrl + H keys. Type the string 'sea' in the Find box and the string 'ocean' in the Replace box and click the Replace All button. The following image shows how to perform this exercise.

Conclusion
There are many ways to find and replace a text string in text files. This tutorial explained the two most straightforward methods for it. These are the sed command and the text editor's built-in text manipulation option. The sed command performs this action on the command prompt. You can use the text editor's built-in text manipulation option on GUI.
By ComputerNetworkingNotes Updated on 2026-05-26