Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change all occurrences of a word in all files in a directory

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?

like image 276
Ryan Ward Avatar asked Jan 18 '12 05:01

Ryan Ward


People also ask

How do you replace a word in all files in a folder?

Remove all the files you don't want to edit by selecting them and pressing DEL, then right-click the remaining files and choose Open all. Now go to Search > Replace or press CTRL+H, which will launch the Replace menu. Here you'll find an option to Replace All in All Opened Documents.

How do you replace a string that occurs multiple times in multiple files inside a directory?

s/search/replace/g — this is the substitution command. The s stands for substitute (i.e. replace), the g instructs the command to replace all occurrences.

How do you search for a word in all files in a directory?

Search All Files in Directory To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command. The output shows the name of the file with nix and returns the entire line.


1 Answers

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.

like image 93
Dan Fego Avatar answered Oct 14 '22 20:10

Dan Fego