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" {} +
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 thefind
command matches.+
: Called batch mode tellsfind
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" {} +
-
After execution result of
test.js
outside“node_modules”
:
XBaraaX mohamed
-
Also after executing we find
test.js
inside“node_modules”
:
baraa mohamed
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
Top comments (0)