44

I was in the process of creating a User class where one of the methods was get_privileges();.

After hours of slamming my head into the keyboard, I finally discovered that the previous coder who I inherited this particular database spelled the word "privileges" as "privelages" in the MySQL database, and thus also everywhere in the hundreds of files that access these "privelages" it is spelled that way.

Is there a way in Linux (Ubuntu Server) that I can go through every place in the /var/www folder and replace "privelages" with "privileges", so that I don't have to deal with this typo and code around it?

sk8forether
  • 247
  • 2
  • 9
Ryan Ward Valverde
  • 6,458
  • 6
  • 37
  • 48

3 Answers3

73

A variation that takes into account subdirectories (untested):

find /var/www -type f -exec sed -i 's/privelages/privileges/g' {} \;

This will find all files (not directories, specified by -type f) under /var/www, and perform a sed command to replace "privelages" with "privileges" on each file it finds.

Dan Fego
  • 13,644
  • 6
  • 48
  • 59
  • 11
    Thanks - for people on a Mac with BSD sed. You'll need to add an extension to the -i argument. Like so: `sed -i "" 's/prive...` – xer0x Jan 27 '12 at 19:27
  • If you only want to run `sed` on the files that contain the misspelling (which you will want to if your project contains thousands of files), you can do `grep -l -r "privelages" /var/www | xargs -I "@" sed -i 's/privelages/privileges/g' @` – gpanders Aug 15 '17 at 19:56
  • using grep and then sed is probably worse performing than just using sed, as it means opening and reading every file twice – cegfault Nov 08 '17 at 07:38
  • 4
    Beware doing this in a directory that has a .git folder. This can corrupt the contents! – Zach Boyd Sep 15 '18 at 17:34
  • I know this question is tagged linux but for Mac, I have to add `-e` after `sed -i`. – owyongsk Dec 11 '18 at 04:09
8

Check this out: http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/

cd /var/www
sed -i 's/privelages/privileges/g' *
cegfault
  • 6,442
  • 3
  • 27
  • 49
  • 1
    You will need to add a `find`, or use a shell which allows you to glob all files in all subdirectories (hint: `**/*` does this in some shells) if you want to recurse subdirectories. This is a FAQ; search for similar questions. Sympathies to you for having to cope with an illiterate predecessor ... – tripleee Jan 18 '12 at 06:04
3

I generally use this short script, which will rename a string in all files and all directory names and filenames. To use it, you can copy the text below into a file called replace_string, run sudo chmod u+x replace_string and then move it into your sudo mv replace_string /usr/local/bin folder to be able to execute it in any directory.

NOTE: this only works on linux (tested on ubuntu), and fails on MacOS. Also be careful with this because it can mess up things like git files. I haven't tested it on binaries either.

#!/usr/bin/env bash

# This will replace all instances of a string in folder names, filenames,
# and within files.  Sometimes you have to run it twice, if directory names change.


# Example usage:
# replace_string apple banana

echo $1
echo $2

find ./ -type f -exec sed -i -e "s/$1/$2/g" {} \;  # rename within files
find ./ -type d -exec rename "s/$1/$2/g" {} \;    # rename directories
find ./ -type f -exec rename "s/$1/$2/g" {} \;  # rename files
matwilso
  • 2,924
  • 3
  • 17
  • 24