perl -MFile::Path -pi'bak/*' -e 'BEGIN { mkpath [ $^I =~ m!(.*/)! ] } s/foo/bar/g' file1 file2 file3 ...