Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

Saturday, November 10, 2007

Unix Command To Delete Corrupted Subversion Meta-data

Did you ever find yourself in a situation where you have projects checked-out from source control and the directory-based source control meta-data (from CVS or Subversion) has somehow become irreparably corrupted (or the fix to the corruption is non-obvious)? You do not want to lose local working-copy changes, but because of the corruption you cannot rebuild the meta-data, re-connect to source control, or overlay freshly-updated project files from the source control system without the potential loss of your local workspace changes?
A solution to this problem is to delete only the project's meta-data directories, back-up the project files to a temporary directory, re-check out the project code from the source control system, and overlay the backed-up project files onto the pristine project (which has uncorrupted meta-data).

This is the simple Unix command I use for deleting all the Subversion meta-data in a directory and in its subdirectories:

  • find . -name .svn -type d -print | xargs rm -r -f

It is pretty straightforward to interpret what is happening with this command. It is finding any directory (-type d) named .svn in the current directory (.) and its subdirectories. Each of those directories is deleted (rm -r) without prompting (-f). -print | xargs can be interpretted as use each result from the find command as input to the rm command.

find is very robust in what it can take as input, so a more general version of this line could be rewritten as:

  • find </path/to/a/directory> -name <a_regex> -type <d | f> -print | xargs rm -r -f

It can then be used to delete from a branch of the filesystem heirarchy any files or directories whose names match some pattern.

Friday, October 26, 2007

Unix Command to Find And Replace Text in a File

The following is a very simple, one-line command for finding and replacing all instances of a text pattern in a file (or files). There are numerous sources for this short script. It may seem completely trivial, but it comes in handy.
  • perl -pi -e 's/<find>/<replace>/' FILE_1 FILE_2 ... FILE_n

This will only replace the first occurrence of the pattern per line. If you want to replace all occurrences in every line, then add the 'g' option, like this.
  • perl -pi -e 's/<find>/<replace>/g' FILE_1 FILE_2 ... FILE_n

If you want the find to be case-insensitive, then use the 'i' option, like this.
  • perl -pi -e 's/<find>/<replace>/i' FILE_1 FILE_2 ... FILE_n

If you find that you need to replace file path names in text, then instead of back-slashing all the path separators in your pattern, you might want to choose a different s/// separator. I have found that '~' works pretty well (but if your paths contain '~'s, then you should experiment).
  • perl -pi -e 's~/path/to/my/file~/new/path/to/file~' FILE_1 FILE_2 ... FILE_n

If you want to replace some text with the contents of an environment variable, then you can access the environment with the hash ENV.

  • perl -pi -e 's~some_text~$ENV{environment_variable}~' FILE_1 FILE_2 ... FILE_n