Find and replace command for whole directory in Linux
In many case most of the projects have so many files in so many different folder. So at some point you want to do find and replace for particular word in your whole project or whole directory in that case this command will help you.
In linux for doing this there is no single command but we can design one new command by combination of several.
find /path/to/directory -type f -exec sed -i.bak 's/oldword/newword/g' {} \;
here it creates a .bak
backup file of every file it processes, in case you need to revert. If you don’t need the backup files at all, change -i.bak
to -i
.
To remove all the backup files when you no longer need them:
find /path/to/directory -type f -name "*.bak" -exec rm -f {} \;
NOTE : if your oldword or newword cantains any special character like & or ‘ or newline then you need to Quote it by adding prefix of \
Example: if you want to replace
ShareProgrammingTips
with
&shareprogrammingtips
then your code will be like
find /path/to/directory -type f -exec sed -i.bak 's/
ShareProgrammingTips
/\&shareprogrammingtips
/g' {} \;
You may like to read this also....
Post comment
Search in this website
our sponsors
latest comments
- Martin on how can I write applications in C or C++ for Android?
- murtaza on Little endian and Big endian in C programming
- Java Interview Questions on Difference between c and c++ and java programming languages
- SUGANYA on How to configure mail from localhost ( wamp ) using PHP?
- Rahul on How to set, clear ,toggle and check a single bit in C?
Find us on Facebook
Top Authors
Find us on stackoverflow
Polls
My Bookmarks
- Audio/video Recorder & player application based on MATLAB
- check dependency of your binary
- defination of all standard c programming language function
- Great Question-Answer on c programming
- know what your c code means
- Limition of c programming language
- List of all version of c programming language
- Online c compiler
- php freelancing work
- some more stuff on C programming language
- Volatile Keyword in Embedded System
- Write Android application in c language