Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

Replacing in files

Your command isn't reliable for several reasonsisn't reliable for several reasons: it excludes all paths containing .git as a substring, it doesn't work with paths containing whitespace or \'", it replaces input with any character between com and foo. The last problem is easily solved by adding a backslash before the dot. The problems with file names may or may not be an issue in your setup — source trees do tend to be tame. Also -name "*" is redundant (it always matches). Here's a safe and simpler alternative that properly omits the .git subtree(s).

find . -name .git -prune -o -type f -exec sed -i 's/com\.foo/org.bar/g' {} +

This still replaces e.g. com.fooalso by org.baralso, which is probably not desirable if it happens. Here's a safer construction:

find . -name .git -prune -o -type f -exec sed -i -e 's/com\.foo$/org.bar/' -e 's/com\.foo\([^-_0-9A-Za-z]\)/org.bar\1/' {} +

Renaming files with zsh

Load the zmv function with autoload -U zmv, then you can use it to rename files based on wildcard patterns. I think what you're looking for is actually

zmv -w '**/com/foo' '$1/org/bar'
zmv -w '**/*com.foo' '$1/${2}org.bar'
zmv '(**/)(*)com.foo([^-_0-9A-Za-z]*)' '$1${2}org.bar$3'

Replacing in files

Your command isn't reliable for several reasons: it excludes all paths containing .git as a substring, it doesn't work with paths containing whitespace or \'", it replaces input with any character between com and foo. The last problem is easily solved by adding a backslash before the dot. The problems with file names may or may not be an issue in your setup — source trees do tend to be tame. Also -name "*" is redundant (it always matches). Here's a safe and simpler alternative that properly omits the .git subtree(s).

find . -name .git -prune -o -type f -exec sed -i 's/com\.foo/org.bar/g' {} +

This still replaces e.g. com.fooalso by org.baralso, which is probably not desirable if it happens. Here's a safer construction:

find . -name .git -prune -o -type f -exec sed -i -e 's/com\.foo$/org.bar/' -e 's/com\.foo\([^-_0-9A-Za-z]\)/org.bar\1/' {} +

Renaming files with zsh

Load the zmv function with autoload -U zmv, then you can use it to rename files based on wildcard patterns. I think what you're looking for is actually

zmv -w '**/com/foo' '$1/org/bar'
zmv -w '**/*com.foo' '$1/${2}org.bar'
zmv '(**/)(*)com.foo([^-_0-9A-Za-z]*)' '$1${2}org.bar$3'

Replacing in files

Your command isn't reliable for several reasons: it excludes all paths containing .git as a substring, it doesn't work with paths containing whitespace or \'", it replaces input with any character between com and foo. The last problem is easily solved by adding a backslash before the dot. The problems with file names may or may not be an issue in your setup — source trees do tend to be tame. Also -name "*" is redundant (it always matches). Here's a safe and simpler alternative that properly omits the .git subtree(s).

find . -name .git -prune -o -type f -exec sed -i 's/com\.foo/org.bar/g' {} +

This still replaces e.g. com.fooalso by org.baralso, which is probably not desirable if it happens. Here's a safer construction:

find . -name .git -prune -o -type f -exec sed -i -e 's/com\.foo$/org.bar/' -e 's/com\.foo\([^-_0-9A-Za-z]\)/org.bar\1/' {} +

Renaming files with zsh

Load the zmv function with autoload -U zmv, then you can use it to rename files based on wildcard patterns. I think what you're looking for is actually

zmv -w '**/com/foo' '$1/org/bar'
zmv -w '**/*com.foo' '$1/${2}org.bar'
zmv '(**/)(*)com.foo([^-_0-9A-Za-z]*)' '$1${2}org.bar$3'
Source Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k

Replacing in files

Your command isn't reliable for several reasons: it excludes all paths containing .git as a substring, it doesn't work with paths containing whitespace or \'", it replaces input with any character between com and foo. The last problem is easily solved by adding a backslash before the dot. The problems with file names may or may not be an issue in your setup — source trees do tend to be tame. Also -name "*" is redundant (it always matches). Here's a safe and simpler alternative that properly omits the .git subtree(s).

find . -name .git -prune -o -type f -exec sed -i 's/com\.foo/org.bar/g' {} +

This still replaces e.g. com.fooalso by org.baralso, which is probably not desirable if it happens. Here's a safer construction:

find . -name .git -prune -o -type f -exec sed -i -e 's/com\.foo$/org.bar/' -e 's/com\.foo\([^-_0-9A-Za-z]\)/org.bar\1/' {} +

Renaming files with zsh

Load the zmv function with autoload -U zmv, then you can use it to rename files based on wildcard patterns. I think what you're looking for is actually

zmv -w '**/com/foo' '$1/org/bar'
zmv -w '**/*com.foo' '$1/${2}org.bar'
zmv '(**/)(*)com.foo([^-_0-9A-Za-z]*)' '$1${2}org.bar$3'