rm has no undo. Since macOS 15 there is a built-in command that sends files to the Trash, just like Command-Delete in Finder.
It takes files and folders, several at once:
trash old-notes.txt build/
trash -v *.log
With -v it prints where each item went, normally your ~/.Trash. From there, Put Back in Finder restores the file to its old folder, same as for anything you deleted by hand. I’d use it for cleanups where you’re only fairly sure.
In a script, -s makes it stop with an error when a move fails, so the script doesn’t continue as if everything was removed.
It lives in /usr/bin/trash and first appeared in macOS 15. On older versions the name may point to a Homebrew package with different options, so check with which trash. Also, the Trash still takes disk space, so for freeing space this alone does nothing until you empty it.
Further reading: trash man page (macOS)