Search code examples
xonsh

Find and replace text in files with xonsh


How would one replace text in all files within a given directory (recursively) while in the xonsh shell?

For example, how would I rewrite the below bash command:

find . -type f -name "*baz*" -exec sed -i 's/foo/bar/g' {} +

Solution

  • I might try this:

    for fpath in pg`*baz*`:
        if not fpath.is_file():
            continue
        fpath.write_text(fpath.read_text().replace('foo', 'bar'))