Change a word in every file recursively in Linux

From The IT Community
Jump to: navigation, search

If you want to edit something like the name of an object in a menu and you know you used that name in a lot of files, you don't want to edit each file by itself. Instead, you would want to just change that one word in all files at once.

Well, you can.

Here is how to change a word in every file recursively in Linux:

  • In your Bash, go to the directory your files are in. If it has subdirectories with files you want to change too, this command will do that for you.
  • Execute the following command:
find . -type f -exec sed -i 's/group/interest/Ig' {} +

What this will do, is to change the word group into the word interest in every file in every subdirectory, starting from the current directory.

The Ig parameter will tell the program to ignore case. If you just want to change the exact case, just get rid of the I:

find . -type f -exec sed -i 's/group/interest/g' {} +

If you want to just change .php files, the syntax would look like this:

find *.php -type f -exec sed -i 's/group/interest/Ig' {} +

Note: To be sure, make a copy of the whole directory you execute the command in.

Was this article helpful? Then please donate to keep The IT Community alive...

If you found this article helpful please share it, comment and help others by writing your own article.

Translate this page: