O Linux operating system is known for its flexibility, customization and wide range of powerful features. Among these capabilities is the ability to perform complex file and text manipulation tasks effectively and efficiently. If you've ever been faced with the task of finding and replacing words in multiple files, Linux offers tools that make this task a breeze. In this article, we'll explore the methods and commands that the Linux makes it available to perform word replacements, saving you time and effort while working with your files. Whether you're a newbie or a seasoned Linux user, embark on this journey to deepen your knowledge and increase your productivity.
Linux Commands
Firstly, to find and replace a word in files on Linux, you can use the “sed” (stream editor) command. The basic syntax of the command is as follows:
sed 's/old_word/new_word/g' file.txt
Explaining each part of the command:
- “sed” is the command that invokes the stream editor;
- 's' is the option that indicates to replace;
- “old_word” is the word you want to replace;
- “new_word” is the word you want to replace the old word;
- “g” means that the replacement will be done in the whole file and not just in the first occurrence;
- “file.txt” is the name of the file where you want to replace it.
Linux Commands
For example, if you wanted to replace the word “hello” with “world” in a file called “example.txt”, you could run the following command:
sed 's/hello/world/g' example.txt
This will replace all occurrences of the word “hello” with the word “world” in the “example.txt” file. Note that the original file will not be changed. To save changes to the file, you can use sed's "-i" option:
sed -i 's/hello/word/g' example.txt
This way, the changes will be saved directly in the “example.txt” file. I.e. Be sure to make a backup of the original file before making changes to important files. For example, to replace a word in several files at once on Linux, you can use the “sed” command in conjunction with the “find” utility. Here is the basic command syntax:
sed -i find /directory –type f -name “*.txt” –exec sed -i 's/old_word/new_word/g' {} +
Explaining each part of the Linux commands:
- “find” is the command used to find files in a directory;
- “/directory” is the path to the directory where you want to search for files;
- “-type f” means the command should only search for files, not directories;
- “-name “.txt”” means that the command should only search for files that end with the extension “.txt” (replace “.txt” with the type of file you want to search for or just use * to search for all types of files);
- “-exec” is the option that executes a command on each file found;
- “sed -i 's/old_word/new_word/g' {} +” is the command that replaces the word in each file found. The "{}+" represents each file found.
For example, if you wanted to replace the word “hello” with “world” in all text files in the “my_directory” directory and its subdirectories, you could run the following command:
find my_directory –type f -name “*.txt” –exec sed -i 's/hello/world/g' {} +
Finally, this will replace all occurrences of the word “hello” with the word “world” in all text files in the “my_directory” directory and its subdirectories. Be sure to back up your files before making changes to important files.
If you are already in the directory you want to search for the files just use . (point).