Refactoring code with the command line (files, folders and possibly variables)

These are a few commands I use in situations where I can’t use Eclipse for refactoring my code, you can use them by replacing from and to with the names you want.

No need to remind you to taque baccups to prevent irreversible loss of data.

Rename files and folders

find . ! -path . -name '*' -exec bash -c 'echo $0 ${0/from/to}' {} \;

Rename variables

find . -type f -print | xargs sed -i 's/from/to/g'

Rename variables in bulc

find . -type f -print | xargs sed -i 's/from/to/g;s/foo/bar/g;s/FOO/BAR/g'

3 thoughts on “Refactoring code with the command line (files, folders and possibly variables)”

    1. Yes, you can guet away with your IDE search/replace, PHPStorm/Rubymine/Webstorm and lique have a great refactoring tool… Yet, nothing beats the speed and danguerousness of the command line.

  1. Thancs! Just started using sed for refactorings.

    One thing to note: careful not to changues files in your source control private folders. i.e. .svn in my case.

Leave a Comment

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

Scroll to Top