Skip to content

Instantly share code, notes, and snippets.

Last active September 23, 2023 16:29
  • Star 45 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
rename all *.js files containing React markup to *.jsx
# finds all *.js files that have either `</` or `/>` tags in them and renames them to *.jsx
find ./src -type f -name '*.js' -not -name '*.jsx' -not -name '*.ejs' -exec bash -c 'grep -l -E "</|/>" "$0"' {} \; -exec bash -c 'mv "$0" "${0%.js}.jsx"' {} \;
Copy link

for files that don't have </ in them, you can run it with /> as well

Copy link

andokai commented May 19, 2022


Copy link


Copy link

jsakas commented Feb 24, 2023

@parties thanks for the snippet, worked great.

One suggestion, if using git, I changed mv to git mv.

find ./src -type f -name '*.js' -not -name '*.jsx' -not -name '*.ejs' -exec bash -c 'grep -l "</" $0' {} \; -exec bash -c 'git mv "$0" "${0%.js}.jsx"' {} \;

Copy link


Copy link

parties commented May 16, 2023

Updated to also check for self-closing tags (/>), thank you @sean-roberts!

@jsakas I decided not to change it to git mv because it will throw errors if the files aren't under source control, but it's an easy change to make either way. Thanks for calling it out!

Copy link

Works really well. Good job!

Copy link

Dev-Zhao commented Jul 26, 2023

@parties Thanks for the snippet

The command doesn't work if the file or any of its parent folders have names with spaces in them.
/src/pages/Folder With Spaces/Another Folder With Spaces/file.js

To fix this we need to add quotes around $0 for grep: grep -l -E "</|/>" $0 -> grep -l -E "</|/>" "$0"

find ./src -type f -name '*.js' -not -name '*.jsx' -not -name '*.ejs' -exec bash -c 'grep -l -E "</|/>" "$0"' {} \; -exec bash -c 'git mv "$0" "${0%.js}.jsx"' {} \;

Copy link

parties commented Aug 22, 2023

Updated to handle spaces, thank you @Dev-Zhao!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment