For a simple single line with -n or -p you want
perl -i~ -pe 's/foo/bar/' *.aspx
-i~ says to change each target file in place and leave the original as a backup with the suffix ~ added to the file name. (Omit the suffix so that it doesn’t leave a backup. But if you are still learning or experimenting, this is a bad idea: deleting backups when you are done is a much smaller problem than restoring originals from a backup if you have something messed up.)
If your Perl code is too complicated for a single line (or useful enough for reuse), replace -e '# your code here' with scriptname.pl ... although it may be refactoring scriptname.pl to accept a list of file name arguments and just use scriptname.pl *.aspx to run it in all *.aspx files in the current directory.
If you need to overwrite the directory structure and find all files with a specific name pattern, it is useful to use the find utility.
find . -name '*.aspx' -exec perl -pi~ -e 's/foo/bar/' {} +
If your find does not support -exec ... + , try with -exec ... \; although it will be slower and start more processes (one for each file that you find, and not as small as possible to process all the files).
To only scan some directories, replace . (which calls the current directory) a space-separated list of directories to examine or even use find to find the directories themselves (and then perhaps examine -execdir to do something in every directory that find selects using complex, complex, critical important to the business, possibly a secret list of find option predicates).
It is also possible to examine find2perl to do this directory recursion natively in Perl.
tripleee Jun 25 '17 at 10:15 2017-06-25 10:15
source share