I need to find and replace a word in all the files across a directory and its subdirectories.
I used this line of code:
perl -pi -w -e 's/foo/bar/g;' *.*
But it changes the words only in files in the current directory and doesn't affect the subdirectories.
How can I change the word on every directory and subdirectory?
You could do a pure Perl solution that recursively traverses your directory structure, but that'd require a lot more code to write.
The easier solution is to use the find
command which can be told to find all files and run a command against them.
find . -type f -exec perl -pi -w -e 's/foo/bar/g;' \{\} \;
(I've escaped the {} and ; just in case but you might not need this)