- How to change all occurrences of a word in all files in a directory
- 3 Answers 3
- How to Replace All Occurrences of a Word in All Files in Linux Command Line
- Solution
- Explanation
- How to replace a string in all folder and file names [duplicate]
- 3 Answers 3
- Find and replace text within multiple files
- 10 Answers 10
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?
3 Answers 3
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.
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.
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’ @
using grep and then sed is probably worse performing than just using sed, as it means opening and reading every file twice
cd /var/www sed -i 's/privelages/privileges/g' *
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 .
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
How to Replace All Occurrences of a Word in All Files in Linux Command Line
Consider a scenario where you have to find all occurrences of a certain text and then replace it with another in all the files. Here’s how to do that in Linux command line.
Question: How do I replace all occurrences of a word in all the files in the given folder?
Consider a problem scenario where you have a website in which you often refer to the current year. Every year you need to update the year. You can easily find all occurrences in all the files but manually changing the year, one by one, in each file should be avoided.
Solution
So, what do you do? One of the several ways to find all occurrences and then replace the searched term is by using a combination of find and sed commands.
You can easily do so with a single command: In this example, you have 50 web pages in the pages folder. This command will instantly change all occurrences of “2017” to “2018”:
find ~/public_html/my_website/pages -type f -exec sed -i 's/2017/2018/g' <> \;
To make the above example a bit more generic, here’s what you need to do:
find -type f -exec sed -i 's///g' <> \;
In the above command, replace , and with the actual data.
Explanation
What you are doing here is that you are using find command to get all the files in the current directories and its sub-directories. The -type f option makes sure that you only get files, not directories.
And then you pass the result of find command using the exec command to the sed command. The sed command searches for the text and replaces it.
The <> \; is part of the exec command. The space between > and \ is mandatory.
This Linux quick tip was suggested by Linux Handbook reader, Jane Hadley. Got a better solution for this problem? Share it in the comments.
How to replace a string in all folder and file names [duplicate]
How can I recursively replace a string in all folders and files’ name with a different string? I am running Red Hat 6 and I can find them with:
find . -type f -exec sed -i 's/string1/string2/g' <> +
how can I replace a string with the mv command? There might be different folders names containing that string. I would need something like «find . -type f -exec sed -i ‘s/string1/string2/g’ <> +» but for folder names
The command you show ( sed ) doesn’t change any file names. It replaces the string inside the file but leaves the file name as it was.
3 Answers 3
find . -type f -exec rename 's/string1/string2/g' <> +
The find . -type f part of the command means to search for all files ( -type f ) in the current directory ( . ).
- The -exec option tells find to execute a command on each file it finds.
- The rename command is used to rename files, and the syntax used here is for the Perl version of rename .
- The ‘s/string1/string2/g’ is a regular expression that specifies what to search for and what to replace it with. In this case, string1 is the string to be replaced, and string2 is the replacement string. The /g at the end means to replace all occurrences of string1 in the filename.
- The <> symbol is a placeholder for the filename that find has found.
- The + at the end of the command tells find to pass multiple filenames at once to the rename command, which is more efficient than invoking rename for each individual file.
So, overall, this command searches for all files in the current directory and executes the rename command to replace all occurrences of string1 with string2 in the filename for each file found.
Find and replace text within multiple files
It will not have the graphical interface but I would urge you to examine sed (man sed). It is the stream editor that has been in existence from the start of UNIX.
10 Answers 10
Here I use sed to replace every occurrence of the word «cybernetnews» with «cybernet» in every file with the extension, c, in the directory, /home/user/directory/.
find /home/user/directory -name \*.c -exec sed -i "s/cybernetnews/cybernet/g" <> \;
A more generic variation where you search recursively from the directory of execution and operate on only regular, readable, writeable files:
find ./ -type f -readable -writable -exec sed -i "s/cybernetnews/cybernet/g" <> \;
Voted up because sed is the best choice IMO, but answer would be more useful if you explained the components of the answer (personally, I’m not familiar with and curious about the <> \;)
@sequoiamcdowell Woo! I missed this! Sorry. The braces mean basically «for each match» and the escaped semi-colon (\;) is to prevent double parsing. We don’t want the bourne-compatible shell and sed trying to parse the command, just sed.
@Christopher how does it work if the word contains spaces like cyber net news ? How can i convert this to cyber net ?
The stream editor,sed, is a powerful utility for this kind of work and is my first choice, however, if you want to do this from an ordinary text editor using an Ubuntu based native application, I would suggest you take a look at Jedit, It is available in the repositories and can be installed by typing in your console:
sudo apt-get install jedit
Start jedit, click the search menu item, in the menu list, click the Search in Directory item, you will be presented with the dialog below:
This is similar to that of Notepad++ and does the same thing, I believe this is what you want.
This was perfect for when I was searching for an entire line of code, and was hesitant to escape every single regex-special character if I had done the same thing with sed .
This doesn’t work on a large number of files, after a couple of hours it hadn’t made one replacement, whereas geany took a few minutes to find all the occurrences. This may not be an efficient search and replace but it warmed the room up nicely.
Another GUI option is regexxer:
Even, If you don’t know regex, you can use this tool as a simple string search and replace tool and it will do the job for you, no need for regex or anything.
perl -pi -e 's/oldtext/newtext/g' *
replaces any occurence of oldtext by newtext in all files in the current folder. However you will have to escape all perl special characters within oldtext and newtext using the backslash.
Check with Geany, it is perfect NPP replacement for Linux. You can do exactly that plus you can use regex.
As far as I can tell, Geany 1.23.1 (packaged with Ubuntu 13.10) allows searching multiple files using regex, but not replacing in them.
You can search and replace in Geany multiple files if you open them all and then using the «in session» button in the Search/Replace dialog.
Ok, The regexxer was pretty much what I had been asking for, I had just skipped it earlier due to its name having regex and I just tend to avoid anything to do with regex. But I was wrong, adn that tool has exactly what is required for search and replace.
A very simple solution: replace in all *.txt files in folder string_1 with string_2 :
sed -i 's/string_1/string_2/g' *.txt
Great answer! Just some addition. If you want to replace a string in any file of the current directory recursively then try this: sed -i ‘s/string_1/string_2/g’ $(grep -rEl ‘string_1’ ./)
I wrote a little script for just this thing. If you only need the basics and are not familiar with sed etc, take a look here: http://www.csrdu.org/nauman/2010/12/30/bash-script-to-find-and-replace-in-a-set-of-files/
The script is the following:
for f in submit_*; do sed "s/old_db_name/new_db_name/" < $f >a_$f ; mv a_$f $f ; done
You can use this script, copy code and make a file find_and_replace_in_files.sh .
I have modified it a little; please tell me your opinion.
# ***************************************************************************************** # find_and_replace_in_files.sh # This script does a recursive, case sensitive directory search and replace of files # To make a case insensitive search replace, use the -i switch in the grep call # uses a startdirectory parameter so that you can run it outside of specified directory - else this script will modify itself! # ***************************************************************************************** !/bin/bash # **************** Change Variables Here ************ startdirectory="/your/start/directory" searchterm="test" replaceterm="test=ok!" # ********************************************************** echo "***************************************************" echo "* Search and Replace in Files Version 01-Aug-2012 *" echo "***************************************************" i=0; for file in $(grep -l -R $searchterm $startdirectory) do cp $file $file.bak sed -e "s/$searchterm/$replaceterm/ig" $file > tempfile.tmp mv tempfile.tmp $file let i++; echo "Modified: " $file done echo " *** All Done! *** Modified files:" $i