Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Breaking News

How to recursively change a word in all site files

When managing a website, you may encounter situations where you need to replace a specific word or domain across multiple files. This task can arise for various reasons, such as updating outdated content, rebranding, or correcting errors. Fortunately, you can achieve this efficiently by using command-line tools to recursively find and replace text within files. This guide will show you how to perform these operations using find, sed, and perl.

Use Cases for Recursively Changing Words

There are several common scenarios where you might need to replace words or domains in multiple files:

  • Replacing a word with another word across all files in a directory.
  • Changing a domain name in a database or configuration files.
  • Updating references to a particular term or URL throughout your site.

Changing a Word Recursively in All Site Files

To change ‘domain1’ to ‘domain2’ in all PHP files, you can use the following find and sed command:

find -type f -name \*.php -exec sed -i -r 's/domain1/domain2/g' {} \;

This command will search for all PHP files in the current directory and its subdirectories, and replace ‘domain1’ with ‘domain2’ within these files.

Similarly, to change ‘domain1’ to ‘domain2’ in all JavaScript files, use:

find -type f -name \*.js -exec sed -i -r 's/domain1/domain2/g' {} \;

Handling Errors with Perl

If you encounter errors such as “argument list too long” when using the above commands, you can try an alternative solution using Perl. This method handles large numbers of files more efficiently:

find . -type f -print0 | xargs -0 perl -pi -e 's/domain1/domain2/g'

This command uses find to print all file names in the current directory and its subdirectories, then pipes them to xargs which passes them to perl for the replacement operation.

Conclusion

Recursively changing a word or domain in all site files can be efficiently managed using command-line tools like find, sed, and perl. These methods are powerful for web developers and administrators who need to perform bulk text replacements. Whether updating content, rebranding, or correcting errors, these commands help ensure consistency and accuracy across your website files.

About iryna

I'm Iryna, a web developer from Ukraine with a decade of experience solving complex technical challenges in the world of freelance. Throughout my career, I've worked on everything from troubleshooting server-side issues and optimizing website performance to enhancing user interfaces. On this blog, I share detailed solutions to the technical problems I’ve encountered and methods that have worked best for me. In addition to my technical expertise, I’m also passionate about digital drawing. I hope the tutorials and insights I provide here will help both fellow developers and creatives alike in their own projects.

Check Also

Load key .ssh/id_rsa: bad permissions

I had a rather minor problem when trying to run the git clone command: Cloning …

Leave a Reply

Your email address will not be published. Required fields are marked *