Check out marcel (
https://marceltheshell.org), which does this exactly. E.g. to explore the current directory recursively, find files changed in the last day, and then compute the sum of file sizes by extension for the qualifying files:
ls -fr | select (f: now() - f.mtime < days(1)) | map (f: (f.suffix, f.size)) | red . +
- ls -fr: list recursively, yielding only files.
- select (f: ...): f is bound to each file from ls. If a file's modification time is within one day of the current time, then write that file object to the output pipeline.
- map (f: ...): f is bound to each file from the select, and mapped to a tuple containing the extension and file size.
- red . +: Group by file extension and sum sizes.
And shelling out from python is much simpler. Here is the same computation in python
from marcel.api import *
for ext, size in (ls(file=True, recursive=True) |
select(lambda f: now() - f.mtime < days(1)) |
map(lambda f: (f.suffix, f.size)) |
red(None, r_plus)):
print(f'{ext}: {size})
marcel.api imports functions matching the commands (ls, select, and so on), and the pipeline yields an iterator, for neat integration with python loops.