DEV Community

Baraa Mohamed
Baraa Mohamed

Posted on • Edited on

Rename Functions, Variables, or Any Text Across Multiple Files with One Command! 🔍🔄

I’ve come into situation before while I was building Employee Management System Full-stack website, Where I needed to change name of variable and some functions but the issue was that they are used in many files.

For them being used in multiple files it wasn’t logically to manually detect them in each file and replace them.

One solution I found is using Linux commands :

find . \( -path "./node_modules"  \) -prune -o -name "*.js" -exec sed -i "s/old_text/new_text/g" {} +

Enter fullscreen mode Exit fullscreen mode
  • find : is used to find certain files, here we use it to find all .js files
  • \( \) : the backslashes are used for Linux to execute what’s inside instead of giving an error
  • -path : To specify certain path which we will prune
  • -prune : To ignore certain paths from this command’s execution
  • -o : Is or option so if left side is executed ( which is ignoring “node_modules” ) we skip executing rest of command after -o .
  • -name "*.js" : to find any file that is .js when it’s not “node_modules” folder.
  • -exec sed : To execute replacing command which is sed.
  • -i : to actually change text in file.
  • {} is a placeholder for the file or directory name that the find command matches.
  • + : Called batch mode tells find to pass multiple matched files to the command in batches instead of running the command once per file.

Usage Example

I have 2 test.js files one of them is in “node_modules” folder, Both of them has text “baraa mohamed”.

So we execute following command.

find . \( -path "./node_modules"  \) -prune -o -name "*.js" -exec sed -i "s/baraa/XBaraaX/g" {} +

Enter fullscreen mode Exit fullscreen mode
  • After execution result of test.js outside “node_modules” :
XBaraaX mohamed
Enter fullscreen mode Exit fullscreen mode
  • Also after executing we find test.js inside “node_modules” :
baraa mohamed
Enter fullscreen mode Exit fullscreen mode

As you can see it did not change as we pruned ( ignored ) the whole “node_modules” directory.

🤍 I'd love to connect with you on LinkedIn—let's grow our network and share ideas! here

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay