find . -type f -name "*.swift" -print0 | xargs -0 sed -i '' 's/old string/new string/g'

The above sed command usage is specific to macOS. While sed is available on various operating systems like macOS, Windows, Linux, and BSD, etc. the exact options and behavior might vary between them. Always consult the documentation specific to your operating system before using.

The find command’s options can be adjusted to suit your specific requirements.

A particularly useful, albeit slightly quirky, flag is -0.

From the man xargs documentation:

-0: Change xargs to expect NUL (\0) characters as separators, instead of spaces and newlines. This is typically used in conjunction with the -print0 option in find(1).

This allows you to handle file paths that contain spaces, such as “System Library”.


discussion